如何在其他包中有java文件时编译多个java文件 [英] how to compile multiple java files when there are java files in other packages
问题描述
我得到编译错误说说javac找不到对象的符号。
我有多个包含运行主程序所需的java文件的包。但似乎试图编译这些一个一个不会工作。它在我的IDE运行良好,但我有兴趣了解如何通过命令提示符。
主程序在drivers文件夹。
我已经尝试按照依赖顺序编译文件,但是没有工作。
Javac 文档提供了所有必要的信息。但是,对命令行构建使用Ant或Maven可能很有用。
此页面提供了一个很好的例子使用第一个javac然后Ant来构建一个简单的项目。
这里是一个示例项目, javac。
项目的树结构如下:
。
├──build
└──src
├──攻击
├──驱动程序
│└──Driver.java
└──异常
└──MyException.java
有两个特殊目录 - build
包含编译的类, src
包含源文件(可能在不同的子目录 - 包)。
以下命令编译整个项目并将结果放入 build
目录中。
javac -sourcepath src -d build src / ** / *。java
-sourcepath src
指定目录 src
作为可以找到所有源的位置由编译器。 -d build
选项告诉编译器放置编译文件的位置。
选项 src / ** / *。java
告诉编译器实际编译哪些文件。在这个特定的情况下,它告诉javac看下来两层,并选择所有的* .java在那个级别。
如果有 *。 / code>文件在不同级别的文件列表需要指定。为此,可以创建这样的列表作为外部文件,并像
javac
的输入选项传递此文件。
这是在Linux / Unix下如何做到的:
find -name* .java source.txt
上述命令创建文件source.txt,其中包含找到的* .java的完整路径文件。对于此示例,它包含:
./ src / drivers / Driver.java
./src/exceptions/MyException .java
为了编译项目,将源文件列表刷入 source.txt
,可以使用以下命令:
javac -d build @source。 txt
请注意 @ source.txt
指定在末尾告诉编译器在哪里查找源文件列表。还请注意,可以省略 -sourcepath
选项。
运行上述命令后,目录结构的更改如下。
。
├──build
│├──驱动程序
││└──Driver.class
│└──异常
│└──MyException.class
└──src
├──攻击
├──驱动程序
│└──Driver.java
└──异常
└──MyException .java
可以观察到 build
目录现在包含相应包中的编译类文件。
并且,如果你想运行它,假设例如 Driver
有方法 main
,以下命令执行程序。
java -cp::build:** / *。class drivers.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 say 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屋!