当其他包中有Java文件时,如何编译多个Java文件? [英] How to compile multiple Java files when there are Java files in other packages?
问题描述
我正在一个目录 (javac *.java
) 中编译多个文件,但是当我尝试这样做时遇到了问题.
我收到编译错误,说 javac
找不到对象的符号.
我有多个包含运行主程序所需的 Java 文件的包.但似乎试图将这些一一编译是行不通的.它在我的 IDE 中运行良好,但我有兴趣通过命令提示符了解它是如何完成的.
主程序位于驱动程序文件夹中.我试过按依赖顺序编译文件,但没有成功.
Javac 文档 提供了所有必要的信息.但是,将 Ant 或 Maven 用于命令行构建可能很有用.
本页面提供了一个先使用 javac 然后使用 Ant 的好例子构建一个简单的项目.
<小时>这是一个示例项目,以及如何使用 javac 对其进行编译.
项目的树状结构是这样的:
.├──构建└── src├── 攻击├── 司机│ └── Driver.java└── 例外└── MyException.java
有两个特殊目录 -- build
用于包含已编译的类和 src
用于包含源文件(可以在不同的子目录 -- 包中).
以下命令编译整个项目并将结果放入build
目录.
javac -sourcepath src -d build src/**/*.java
-sourcepath src
指定目录src
作为编译器可以找到所有源代码的地方.-d build
选项告诉编译器在哪里放置已编译的文件.
Option src/**/*.java
告诉编译器实际编译哪些文件.在这种特定情况下,它告诉 javac 向下查找两个级别并选择该级别的所有 *.java.
如果存在不同级别的 *.java
文件,则需要指定文件列表.为此,可以创建这样的列表作为外部文件,并将这些文件作为 javac
的输入选项传递.
这是在 Linux/Unix 下如何做到的:
find -name "*.java" >源文件.txt
上述命令创建文件 source.txt,其中包含找到的 *.java 文件的完整路径.对于此示例,它包含:
./src/drivers/Driver.java./src/exceptions/MyException.java
为了编译带有刷新到source.txt
中的源文件列表的项目,可以使用以下命令:
javac -d build @source.txt
请注意 @source.txt
在末尾指定,它告诉编译器在哪里查找源文件列表.另请注意,可以省略 -sourcepath
选项.
以下是运行上述命令后目录结构的变化.
<预><代码>.├──构建│ ├── 驱动程序│ │ └── Driver.class│ └── 例外│ └── MyException.class└── src├── 攻击├── 司机│ └── Driver.java└── 例外└── MyException.java可以看出,build
目录现在包含各个包中已编译的类文件.
而且,如果您想运行它,例如,假设 Driver
具有方法 main
,则以下命令将执行该程序.
java -cp .:build:**/*.classdrivers.Driver
请注意文件分隔符:
(冒号)在Unix下使用,Windows下改为;
(分号).
I am compiling multiple files in a directory (javac *.java
) but I have a problem when I try to do this.
I get compile errors saying that javac
cannot find a symbol of an object.
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.
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 documentation provides all the necessary information. However, it might be useful to use Ant or Maven for command line builds.
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
There are two special directories -- build
for containing compiled classes and src
to contain source files (could be in different subdirectories -- packages).
The following command compiles the whole project and puts the result into the build
directory.
javac -sourcepath src -d build src/**/*.java
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.
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.
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
.
Here is how this could be done under Linux/Unix:
find -name "*.java" > source.txt
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
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
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
As can be observed the build
directory now contains the compiled class files in respective packages.
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
Please note that file separator :
(colon) is used under Unix, for Windows change it to ;
(semicolon).
这篇关于当其他包中有Java文件时,如何编译多个Java文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!