其他软件包中有Java文件时,如何编译多个Java文件? [英] How to compile multiple Java files when there are Java files in other packages?
问题描述
我正在编译目录( javac * .java
)中的多个文件,但是在尝试执行此操作时遇到问题。
I am compiling multiple files in a directory (javac *.java
) but I have a problem when I try to do this.
我收到编译错误,指出 javac
找不到对象的符号。
I get compile errors saying that javac
cannot find a symbol of an object.
我有多个包含Java文件的软件包运行主程序所需的内容。但是,似乎无法一一编译这些文件。它可以在我的IDE中正常运行,但是我有兴趣通过命令提示符了解其操作方式。
I have multiple packages that contain Java files that are needed to run the main program. But it seems that trying to compile these one by one won't work. It runs fine in my IDE but I'm interested in learning how it's done via command prompt.
主程序位于drivers文件夹中。我已经尝试按照依赖关系的顺序编译文件,但是没有解决。
The main program is in the drivers folder. I have tried compiling the files in order of dependency but that didn't work out.
推荐答案
Javac 文档提供了所有必要的信息。但是,将Ant或Maven用于命令行构建可能会很有用。
Javac documentation provides all the necessary information. However, it might be useful to use Ant or Maven for command line builds.
此页面提供了一个先使用javac然后使用Ant构建简单项目的好例子。
This page provides a good example of using first javac and then Ant for building a simple project.
这里是一个示例项目,以及如何
Here is an example project and how can it be compiled with javac.
项目的树结构是这样的:
The tree structure of the project is this:
.
├── build
└── src
├── attacks
├── drivers
│ └── Driver.java
└── exceptions
└── MyException.java
有两个特殊目录- build
用于包含编译的类,而 src
用于包含源文件(可以在不同的子目录-包中)。
There are two special directories -- build
for containing compiled classes and src
to contain source files (could be in different subdirectories -- packages).
以下命令编译整个项目,并将结果放入 build
目录。
The following command compiles the whole project and puts the result into the build
directory.
javac -sourcepath src -d build src/**/*.java
-sourcepath src
指定目录 src
作为可以找到所有源的位置由编译器。 -d build
选项告诉编译器将编译文件放置在何处。
The -sourcepath src
specifies directory src
as the place where all the source can be found by the compiler. The -d build
options tells the compiler where to place the compiled files.
选项 src / ** / *。java
告诉编译器实际要编译的文件。在这种特定情况下,它告诉javac向下看两个级别并在该级别选择所有* .java。
Option src/**/*.java
tells the compiler what files to actually compile. In this specific case it tells javac to look two levels down and pick all *.java at that level.
如果存在 *。java
文件的级别与文件列表的级别不同。为此,可以创建这样的列表作为外部文件,并按照 javac
的输入选项传递此文件。
If there are *.java
files at different levels than a list of files needs to be specified. For this, one could create such listing as an external file and pass this files as in input option for javac
.
在Linux / Unix下可以这样做:
Here is how this could be done under Linux/Unix:
find -name "*.java" > source.txt
上述命令创建文件source.txt,其中包含找到的* .java的完整路径文件。对于此示例,它包含:
The above command creates file source.txt that contains full paths for the found *.java files. For this example it contains:
./src/drivers/Driver.java
./src/exceptions/MyException.java
为了编译项目,并将源文件列表刷新到 source.txt
,可以使用以下命令:
In order to compile the project with the list of source files flushed into source.txt
, the following command can be used:
javac -d build @source.txt
请注意, @ source.txt
在末尾指定,告诉编译器在哪里寻找源文件列表。另请注意,可以省略 -sourcepath
选项。
Please note that @source.txt
specified at the end that tells the compiler where to look for a list of source files. Please also note that the -sourcepath
option can be omitted.
这是运行上述命令后目录结构的变化。
Here is how the directory structure changed after running the above command.
.
├── build
│ ├── drivers
│ │ └── Driver.class
│ └── exceptions
│ └── MyException.class
└── src
├── attacks
├── drivers
│ └── Driver.java
└── exceptions
└── MyException.java
可以看到 build
目录现在包含各个程序包中的已编译类文件。
As can be observed the build
directory now contains the compiled class files in respective packages.
并且,如果您要运行它,例如,假设 Driver
具有方法 main
,以下命令将执行程序。
And, if you'd like to run it, assuming, for example, that Driver
has method main
, the following command executes the program.
java -cp .:build:**/*.class drivers.Driver
请注意,Unix下使用文件分隔符:
(冒号),对于Windows,将其更改为;
(分号)。
Please note that file separator :
(colon) is used under Unix, for Windows change it to ;
(semicolon).
这篇关于其他软件包中有Java文件时,如何编译多个Java文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!