How to specify for javac where the compiled classes should be?

I compile the source file as follows:

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

How do I change this command line to generate a class file in /home/bob/class?

Also, how should environment variables be set (e.g. JAVA_HOME, CLASSPATH, JAVAPATH)?

+3
source share
3 answers

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

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

The value of the "target directory" specified by the -d option.

The package that the class defined in a.java belongs to. This is specified by the package operator in the java source file.

, , ,

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.

1: , . ,

:

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

, , , , , , Java.

CLASSPATH: /jar , , , , . javac dot-java dot-class. java java-/. , -classpath javac/java.

JAVAPATH: /jar , , , java, . javac. , -sourcepath javac.

JAVA_HOME: "root" - Java. /home/bob/java/jdk 1.5.0_06 JAVA_HOME. , Java. , javac java, JAVA_HOME, . JAVA_HOME script .

, . Java 10 . . ? , , , , .

+6

-d :

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

java- (javac) .

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

/home/bob/class/Test.class

.

+3

Source: https://habr.com/ru/post/1737904/


All Articles