构建 Java 包(javac 到所有文件)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2356443/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:30:24  来源:igfitidea点击:

Building Java package (javac to all files)

javajavac

提问by Vytas

How to compile all files in directory to *.class files?

如何将目录中的所有文件编译为 *.class 文件?

采纳答案by Jon Skeet

Well, this seems pretty obvious, so I may be missing something

嗯,这看起来很明显,所以我可能遗漏了一些东西

javac *.java

(With appropriate library references etc.)

(带有适当的库参考等)

Or perhaps:

也许:

javac -d bin *.java

to javac create the right directory structure for the output.

javac 为输出创建正确的目录结构。

Were you looking for something more sophisticated? If so, could you give more details (and also which platform you're on)?

你在寻找更复杂的东西吗?如果是这样,您能否提供更多详细信息(以及您在哪个平台上)?

回答by Kilian Foth

Here's a code fragment that I use to build an entire project where, as usual, source files are in a deeply nested hierarchy and there are many .jar files that must go into the classpath (requires UNIX utilities):

这是我用来构建整个项目的代码片段,其中源文件像往常一样位于深度嵌套的层次结构中,并且有许多 .jar 文件必须进入类路径(需要 UNIX 实用程序):

CLASSPATH=
for x in $(find | grep jar$); do CLASSPATH="$CLASSPATH:$x"; done
SRC=$(find | grep java$)
javac -cp "$CLASSPATH" $SRC

回答by Kipton Barros

Yet another way using "find" on UNIX is described here:

此处描述了在 UNIX 上使用“查找”的另一种方法:

http://stas-blogspot.blogspot.com/2010/01/compile-recursively-with-javac.html

http://stas-blogspot.blogspot.com/2010/01/compile-recursively-with-javac.html

The following two commands will compile all .java files contained within the directory ./srcand its subdirectories:

以下两个命令将编译目录./src及其子目录中包含的所有 .java 文件:

find ./src -name *.java > sources_list.txt
javac -classpath "${CLASSPATH}" @sources_list.txt

First, findgenerates sources_list.txt, a file that contains the paths to the Java source files. Next, javaccompiles all these sources using the syntax @sources_list.txt.

首先,find生成sources_list.txt一个包含 Java 源文件路径的文件。接下来,javac使用语法编译所有这些源代码@sources_list.txt