java 编译器选项,即 javac -d
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5921042/
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
java compiler options i.e. javac -d
提问by Guy Rich
I'm compiling a program that has a package statement. e.g.
我正在编译一个包含 package 语句的程序。例如
package APPC_LU62.Runtime ;
I also have a pre-existing directory structure that matches the package statement.
我还有一个与 package 语句匹配的预先存在的目录结构。
C:\APPC_LU62\Runtime
How do I keep the javac compiler from creating the same directory structure within the pre-existing one ? i.e.
如何防止 javac 编译器在预先存在的目录结构中创建相同的目录结构?IE
C:\APPC_LU62\Runtime\APPC_LU62\Runtime
It seems to me the compiler ought to be "smart" enough to check first for an existing directory structure before creating one.
在我看来,编译器应该足够“聪明”,可以在创建目录结构之前先检查现有目录结构。
Thanks
谢谢
回答by Pa?lo Ebermann
In general, the compiler expects the source files and outputs the class files according to the package structure.
一般情况下,编译器需要源文件,并根据包结构输出类文件。
If you don't give any -sourcepath
(or -classpath
if no sourcepath is given) options, the source files are searched relative to the current directory. If a source path is given, the source files are searched relative to this path (in addition to any file directly specified on the command line).
如果您不提供任何-sourcepath
(或未-classpath
提供源路径)选项,则将相对于当前目录搜索源文件。如果给出了源路径,则相对于该路径搜索源文件(除了在命令行中直接指定的任何文件)。
Similarly, if you don't specify any -d
options, the class files will be put into directories according to the package structure, relative to the current directory. If you give an -d
option, the class files will be put relative to the directory given by the option. Non-existing directories will be created here.
同样,如果您不指定任何-d
选项,则类文件将根据包结构放入相对于当前目录的目录中。如果你给出一个-d
选项,类文件将相对于选项给出的目录放置。不存在的目录将在此处创建。
Thus, if you want to create the output in the same directory tree as your source files are, the usual way to go would be to change into the root of this tree (C:\
in your case), and from there call javac:
因此,如果您想在与源文件相同的目录树中创建输出,通常的方法是更改为该树的根目录(C:\
在您的情况下),然后从那里调用 javac:
javac -classpath ... -sourcepath . APPC_LU62\Runtime\*.java
(or list only the java files you actually want to compile). Alternatively, you could add the -d C:\
and -sourcepath C:\
options, and then call the command from whereever you want:
(或仅列出您实际要编译的 java 文件)。或者,您可以添加-d C:\
和-sourcepath C:\
选项,然后从您想要的任何位置调用命令:
javac -classpath ... -sourcepath C:\ -d C:\ C:\APPC_LU62\Runtime\*.java
The same is valid later for executing the classes with the java
command: This also expects the classes in directories according to the package structure, with the root being a directory or jar file mentioned in the class path. Thus you will have to add C:\
to the -classpath
for your java
call.
后面同样适用于使用java
命令执行类:这也需要根据包结构在目录中的类,根是类路径中提到的目录或jar文件。因此,您将不得不添加C:\
到-classpath
您的java
通话中。
(By the way, I would not use the root of some drive as the root of the package hierarchy - better move everything one directory down.)
(顺便说一句,我不会使用某个驱动器的根目录作为包层次结构的根目录 - 最好将所有内容向下移动一个目录。)
回答by Mat
Would have been better if you had actually posted your javac
command and stated clearly where your sources are located, but here goes anyway: when you issue javac -d somedir blahblah.java
, javac
will create the appropriate directory structure (matching package name hierarchy) starting atdirectory somedir
.
如果您实际发布了您的javac
命令并清楚地说明了您的源所在的位置会更好,但无论如何:当您发出时javac -d somedir blahblah.java
,javac
将创建适当的目录结构(匹配包名称层次结构)从目录somedir
.
So in your case, you simply need to do:
因此,在您的情况下,您只需要执行以下操作:
javac -d c:\ your_files.java