java 如何为 javac 指示编译的类应该去哪里?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2487001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to indicate for javac where the compiled classes should go?
提问by Hulk
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?
我如何更改此命令行以在/home/bob/class.
Also, how should the environment variables (like JAVA_HOME, CLASSPATH, JAVAPATH) be set for this to work?
此外,应如何设置环境变量(如 JAVA_HOME、CLASSPATH、JAVAPATH)以使其工作?
回答by Pablo Santa Cruz
回答by vkraemer
There are a number of ways to get javac to create a class file /home/bob/class/a.class.
有多种方法可以让 javac 创建类文件 /home/bob/class/a.class。
There are two components that interact to determine where the output of javac will be generated:
有两个组件相互作用以确定 javac 的输出将在何处生成:
The value of the 'destination directory', which is set by the -d option.
“目标目录”的值,由 -d 选项设置。
The package that the class defined in a.java 'belongs to'. This set by the package statement in your java source file.
a.java 中定义的类“属于”的包。这由 java 源文件中的 package 语句设置。
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
注 1:虽然可以在包声明中放置任何值,但不建议这样做。推荐的做法是通过源文件的布局来模拟类的包结构,相对于某些“包根”目录
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.
您引用的每个环境变量都有一个特定的“工作”,当您了解更多有关 Java 开发的知识时,这可能对完成一些任务很有用。
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.
CLASSPATH:假定为包根目录的文件夹/jar 文件列表,用于查找代码所依赖的类文件。javac 使用它来将您的 dot-java 文件转换为 dot-class 文件,并且 java 使用它来运行您的 java 类/程序。如果您使用 javac/java 的 -classpath 选项,则会忽略此变量。
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.
JAVAPATH:假定为包根目录的文件夹/jar 文件列表,用于查找代码所依赖的 Java 源文件。这由 javac 命令使用。如果您使用 javac 的 -sourcepath 选项,则会忽略此变量。
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.
JAVA_HOME:这是您的 Java 安装的“根”目录。在您的问题中,/home/bob/java/jdk1.5.0_06 是 JAVA_HOME。此变量通常由以 Java 语言实现的程序使用。像 javac 和 java 这样的工具不使用 JAVA_HOME,因为它们可以在执行时计算它。大多数时候,人们在他们项目的 shell 脚本中使用 JAVA_HOME 来启动他们的程序。
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.
我不鼓励您使用您引用的环境变量。我已经做了大约 10 年的 Java 编程,并且很少设置它们。我倾向于使用命令行选项。为什么?因为很容易忘记您的变量设置是什么,最终会出现您认为正在发生的情况,而不是。
回答by RubyDubee
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
对于其他标志,您需要设置环境变量。检查这个

