开发者

How to indicate for javac where the compiled classes should go?

开发者 https://www.devze.com 2022-12-24 16:39 出处:网络
I am compiling my source file like this: /home/bob/java/jdk1.5.0_06/bin/javac /home/bob/output/a.java How do I have to change this command line to generate the classfile in /home/bob/class?

I am compiling my source file like this:

/home/bob/java/jdk1.5.0_06/bin/javac /home/bob/output/a.java

How do I have to change this command line to generate the classfile in /home/bob/class?

Also, how should the environment variables (like开发者_高级运维 JAVA_HOME, CLASSPATH, JAVAPATH) be set for this to work?


There are a number of ways to get javac to create a class file /home/bob/class/a.class.

There are two components that interact to determine where the output of javac will be generated:

The value of the 'destination directory', which is set by the -d option.

The package that the class defined in a.java 'belongs to'. This set by the package statement in your java source file.

Here is a table of the possible values that will get you the results you are looking for, with some notes about them

destination directory (-d) |  package declaration |           comments
 /home/bob/classes         |       not set        | you probably have this
 /home/bob                 |       classes        | not recommended. See note 1.
 /home                     |     bob.classes      | not recommended.
 /                         |   home.bob.classes   | not recommended.

Note 1: While it is possible to put just about ANY value in the package declaration, it is not recommended. The recommended practice is to have the package structure of your classes be mimiced by the layout of your source files, relative to some 'package root' directory

Here are some examples:

package declaration |   package root   | recommended path of source file
     z              | /home/b/project1 |   /home/b/project1/z/MyClass.java
    y.z             | /home/b/project1 |   /home/b/project1/y/z/MyClass.java

The environment variable that you reference each have a specific 'job' that will probably be useful in doing some assignments as you learn more about java development.

CLASSPATH: a list of folders/jar-files that are assumed to be package roots for finding class files that your code depends on. This is used by javac to translate your dot-java file into a dot-class file AND it is used by java to run your java class/program. This variable is ignored if you use the -classpath option of javac/java.

JAVAPATH: a list of folders/jar-files that are assumed to be package roots for finding java source files that your code depends on. This is used by the javac command. This variable is ignored if you use the -sourcepath option of javac.

JAVA_HOME: this is the 'root'directory of your java installation. In your question, /home/bob/java/jdk1.5.0_06 is the JAVA_HOME. This variable is usually used by programs that are implemented in the Java language. The tools like javac and java do not use JAVA_HOME, since they can calculate it as they execute. Most of the time, folks use JAVA_HOME in their project's shell script to start their program.

I would discourage you from using the environment variable the you reference. I have been doing java programming for about 10 years and have rarely set them. I tend to use the command line options. Why? Because it is too easy to forget what your variable settings are and end up in situations where what you think is happening, isn't.


You can use -d flag to specify output directory:

$ javac -d /home/bob/class /home/bob/output/a.java

For a complete list of java compiler (javac) options check this link out.


javac -d /home/bob/class Test.java

Will put your generated class file to the specified directory

/home/bob/class/Test.class

And for other flags, you need to set environment variables. Check this

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号