简单的命令行 Java 编译

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

Easy command line Java compile

javacommand-linecompilation

提问by nash

So I have to send a java project to someone outside our company who has no experience with java and they need to compile it. Is there an easy way to do it on the windows command line that does not require writing out lists of the files?

所以我必须将一个java项目发送给我们公司以外没有java经验的人,他们需要编译它。是否有一种简单的方法可以在不需要写出文件列表的 Windows 命令行上执行此操作?

Personally I think javac should be smart enough to handle

我个人认为 javac 应该足够聪明来处理

javac *

when in the folder just under the root of the package hierarchy. Is there anything in this ballpark?

当位于包层次结构根目录下的文件夹中时。这个球场里有什么东西吗?

Edit: The source folder structure is complex and the is no single entry class so some of the ideas mentioned so far won't work. Thanks though! Think 9 levels deep with code on many levels.

编辑:源文件夹结构很复杂,而且不是单一的条目类,所以到目前为止提到的一些想法将不起作用。不过还是谢谢!用多层次的代码思考 9 个层次。

采纳答案by JustJeff

From the folder that represents the base of your package hierarchy, assuming your entry point class is called Main, in a package called app,

从代表包层次结构基础的文件夹中,假设您的入口点类名为 Main,在名为 app 的包中,

javac -classpath . app/Main.java

should generate the correct class definitions. The compiler will ferret out the dependencies and compile whatever other classes are needed. The class files will appear in the same directory as their source files.

应该生成正确的类定义。编译器将找出依赖项并编译任何其他需要的类。类文件将出现在与其源文件相同的目录中。

If, as you say, you have 'more than one entry' class, you will have to at least identify all those top level classes from the dependency hierarchy, which can be listed as further params to javac, specifying the packages as they occur. Like so, assuming you also need to start with other.Entry

如果,如您所说,您有“多个条目”类,则您至少必须从依赖关系层次结构中识别所有这些顶级类,这些类可以作为 javac 的进一步参数列出,并在它们出现时指定包。像这样,假设您还需要从 other.Entry 开始

javac -classpath . app/Main.java other/Entry.java 

Note that you will still have to figure out which of your classes are tops of independent dependency hierarchies, whether you are creating an ant script or doing it this way.

请注意,您仍然需要弄清楚哪些类是独立依赖层次结构的顶部,无论您是创建 ant 脚本还是以这种方式进行操作。

回答by Stephen C

Provide them with an Ant script that does the build with the correct libraries on the classpath, etc. The script can also do other tasks such as building JARs, etc.

为他们提供一个 Ant 脚本,该脚本使用类路径上的正确库等进行构建。该脚本还可以执行其他任务,例如构建 JAR 等。

This requires that that person downloads and installs Ant, but that is not hard. (And there is nothing to stop you from providing them with an appropriate Ant distro to install. Or even sending them a distro ZIP file that has a copy of Ant "preinstalled" in the tree.)

这需要那个人下载并安装 Ant,但这并不难。(并且没有什么可以阻止您为他们提供合适的 Ant 发行版以供安装。或者甚至向他们发送一个发行版 ZIP 文件,该文件在树中“预安装”了 Ant 的副本。)

Providing an Ant script means that you avoid them falling into Java newbie traps such as forgetting to set the classpath, being in the wrong directory, forgetting to recompile dependent files and so on. Plus, it is more "professional".

提供 Ant 脚本意味着您可以避免它们落入 Java 新手陷阱,例如忘记设置类路径、位于错误的目录中、忘记重新编译依赖文件等。另外,它更“专业”。

回答by Cheeso

javac BaseProgram.javawill compile BaseProgram.java from the current directory, and all classes it references that are available in source code, in the same directory tree.

javac BaseProgram.java将从当前目录编译 BaseProgram.java 及其引用的所有类,这些类在源代码中可用,位于同一目录树中。

If BaseProgram references Class1 and Class2, and they are available in Class1.java and Class2.java in the same directory, then they too will get compiled. Likewise if they are in a package, and the package directory is available, they will be compiled.

如果 BaseProgram 引用 Class1 和 Class2,并且它们在同一目录中的 Class1.java 和 Class2.java 中可用,那么它们也会被编译。同样,如果它们在一个包中,并且包目录可用,它们将被编译。

回答by Robert Conn

You can build a file listing all the classes you want to compile (extracted from the javac man page) -

您可以构建一个文件,列出要编译的所有类(从 javac 手册页中提取)-

Example - Two Arg Files You can create two argument files -- one for the javac options and the other for the source file- names: (Notice the following lists have no line-continuation characters.)

   Create a file named options containing:

          -d classes
          -g
          -sourcepath /java/pubs/ws/1.3/src/share/classes

   Create a file named classes containing:

          MyClass1.java
          MyClass2.java
          MyClass3.java

   You would then run javac with:

          % javac @options @classes

示例 - 两个 Arg 文件 您可以创建两个参数文件——一个用于 javac 选项,另一个用于源文件名:(注意以下列表没有行继续符。)

   Create a file named options containing:

          -d classes
          -g
          -sourcepath /java/pubs/ws/1.3/src/share/classes

   Create a file named classes containing:

          MyClass1.java
          MyClass2.java
          MyClass3.java

   You would then run javac with:

          % javac @options @classes

Or you can use *.java on the command line e.g.

或者你可以在命令行上使用 *.java 例如

javac greetings/*.java

(Again from man javac)

(再次来自man javac

Or why don't you just compile the source into a jar that your customer can run using their JRE - especially considering they are not Java savvy?

或者您为什么不将源代码编译成一个 jar,您的客户可以使用他们的 JRE 运行 - 特别是考虑到他们不熟悉 Java?

回答by Philip Bergstr?m

A simple way would be by using:

一个简单的方法是使用:

javac *.java.