Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

making a java package in the command line

Though it's probably reccomended one uses and IDE for coding advanced java projects, I personally prefer running almost entirely command-line (using gedit as a text-editor). So please don't just tell me "Just use eclipse!" or something :P

My question is what the method of creating a package in java is by a command.

I'm not talking about packaging an application that runs in the command line, I'm talking about making a package in the command line. Am I making a text file? Am I making a directory?

Relatedly, how does one link to related libs and natives without use of an IDE?

I know I'm being really awkward here, but I really prefer the control one gets working in command line.

like image 451
gossfunkel Avatar asked Jun 10 '12 19:06

gossfunkel


People also ask

How do I create a Java package?

To create a package, you choose a name for the package (naming conventions are discussed in the next section) and put a package statement with that name at the top of every source file that contains the types (classes, interfaces, enumerations, and annotation types) that you want to include in the package.

Which command creates a package in Java?

The package keyword is used to create a package in java.

Can we create our own package in Java?

Java has some predefined packages and also allows us to create our own package. So, it is possible that a programmer can create a class with the same name as a package that already contains that type in a predefined package.


1 Answers

There are three parts to it: (1) create directory structure; (2) indicate package in java file; (3) compile it.

For example, if you want to create package com.mycompany.myproject, then you need to start in the base directory for your project and then:

(1) create directory com/mycompany/myproject

(2) create java files in that directory, stating package com.mycompany.myproject in them;

(3) compile the files, for example, with javac -cp . com/mycompany/myproject/*.java

You may want to specify a different output directory so as to not mix sources and compiled classes.

If you need to use external libraries (.jar files) to compile, then you need to use -cp or -classpath command-line parameter to javac tool to specify them, e.g.

javac -cp .:some_library.jar:lib/another_library.java com/mycompany/myproject/*.java

It may be a good idea to put all external libraries in one place, e.g. lib subdirectory of your main project directory. And, by the way, the above javac command assumes unix-like environment. If you're on Windows, then you'll need to use ; for path separation.

like image 93
Aleks G Avatar answered Nov 06 '22 04:11

Aleks G