javac 选项递归编译给定目录下的所有 java 文件
我正在使用 javac 编译器来编译项目中的 java 文件。这些文件分布在多个包中,如下所示:com.vistas.util
、com.vistas.converter
、com.vistas.LineHelper
、<代码>com.current.mdcontect。
每个包都有几个 java 文件。我像这样使用javac:(
javac com/vistas/util/*.java com/vistas/converter/*.java
com.vistas.LineHelper/*.java com/current/mdcontect/*.java
一行)
我如何要求编译器从父com目录递归编译所有java文件,而不是给出这么多路径?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
我还建议使用某种构建工具(Ant 或 Maven,已经建议使用 Ant,并且更容易上手)或一个处理编译的 IDE(Eclipse 使用带有协调策略的增量编译,您甚至不必按任何“编译”按钮)。
使用 Javac
如果您需要尝试较大的项目,并且附近没有任何合适的构建工具,您始终可以使用 javac 提供的一个小技巧:可以在以下位置指定要编译的类名一个文件。您只需将文件名以
@
前缀传递给javac
即可。如果您可以创建项目中所有
*.java
文件的列表,那就很简单:sources.txt
文件,这很容易忘记(因此会出现错误 -容易)和令人厌烦的任务。使用构建工具
从长远来看,最好使用专为构建软件而设计的工具。
使用 Ant
如果您创建一个简单的
build.xml
文件来描述如何构建软件:您可以通过运行以下命令来编译整个软件:
使用 Maven
Maven 的设置和使用并不是那么简单,但学习它会带来丰厚的回报。这是一个很棒的教程,可以在 5 分钟内启动一个项目。
使用 IDE
现在什么可以提高您的开发效率。有一些开源替代方案(例如 Eclipse 和 NetBeans,我更喜欢前者),甚至商业的(例如 IntelliJ< /a>) 非常流行且功能强大。
他们可以在后台管理项目构建,因此您不必处理所有命令行内容。然而,如果您知道后台实际发生的情况,那么它总是会很方便,这样您就可以查找
ClassNotFoundException
等偶尔出现的错误。附加说明
对于较大的项目,始终建议使用 IDE和构建工具。前者可以提高您的工作效率,而后者则可以在项目中使用不同的 IDE(例如,Maven 可以使用简单的
mvn eclipse:eclipse
命令生成 Eclipse 项目描述符)。此外,拥有一个可以使用单行命令进行测试/构建的项目很容易介绍给新同事以及例如持续集成服务器。小菜一碟:-)I would also suggest using some kind of build tool (Ant or Maven, Ant is already suggested and is easier to start with) or an IDE that handles the compilation (Eclipse uses incremental compilation with reconciling strategy, and you don't even have to care to press any "Compile" buttons).
Using Javac
If you need to try something out for a larger project and don't have any proper build tools nearby, you can always use a small trick that
javac
offers: the classnames to compile can be specified in a file. You simply have to pass the name of the file tojavac
with the@
prefix.If you can create a list of all the
*.java
files in your project, it's easy:sources.txt
file each time you create a new source or rename an existing one file which is an easy to forget (thus error-prone) and tiresome task.Using a build tool
On the long run it is better to use a tool that was designed to build software.
Using Ant
If you create a simple
build.xml
file that describes how to build the software:you can compile the whole software by running the following command:
Using Maven
Maven is not that trivial to set up and work with, but learning it pays well. Here's a great tutorial to start a project within 5 minutes.
Using an IDE
Now that what could boost your development productivity. There are a few open source alternatives (like Eclipse and NetBeans, I prefer the former) and even commercial ones (like IntelliJ) which are quite popular and powerful.
They can manage the project building in the background so you don't have to deal with all the command line stuff. However, it always comes handy if you know what actually happens in the background so you can hunt down occasional errors like a
ClassNotFoundException
.One additional note
For larger projects, it is always advised to use an IDE and a build tool. The former boosts your productivity, while the latter makes it possible to use different IDEs with the project (e.g., Maven can generate Eclipse project descriptors with a simple
mvn eclipse:eclipse
command). Moreover, having a project that can be tested/built with a single line command is easy to introduce to new colleagues and into a continuous integration server for example. Piece of cake :-)有点残酷,但工作起来却像地狱一样。 (仅在小程序上使用,效率绝对不高)
Kinda brutal, but works like hell. (Use only on small programs, it's absolutely not efficient)
在通常情况下,如果您想要编译整个项目,您可以简单地为主类提供 javac 并让它编译所有必需的依赖项:
javac -sourcepath 。路径/to/Main.java
In the usual case where you want to compile your whole project you can simply supply javac with your main class and let it compile all required dependencies:
javac -sourcepath . path/to/Main.java
如果你的 shell 支持它,这样的东西会起作用吗?
如果您的 shell 不支持
**
,那么也许可以工作(对于具有 3 个组件的所有软件包 - 或多或少进行调整)。
If your shell supports it, would something like this work ?
If your shell does not support
**
, then maybeworks (for all packages with 3 components - adapt for more or less).
javac -cp "jar_path/*" $(find . -name '*.java')
(我不喜欢使用 xargs,因为它可以将它们分割并多次运行 javac,每次都有一个子集java 文件,其中一些可能会导入同一 javac 命令行上未指定的其他文件)
如果您有 App.java 入口点,则使用 -sourcepath 的怪胎方式是最好的。它按照导入依赖关系编译它需要的所有其他 java 文件。例如:
javac -cp "jar_path/*" -sourcepath src/ src/com/companyname/modulename/App.java
您还可以指定目标类文件目录: -d target/ 。
javac -cp "jar_path/*" $(find . -name '*.java')
(I prefer not to use xargs because it can split them up and run javac multiple times, each with a subset of java files, some of which may import other ones not specified on the same javac command line)
If you have an App.java entrypoint, freaker's way with -sourcepath is best. It compiles every other java file it needs, following the import-dependencies. eg:
javac -cp "jar_path/*" -sourcepath src/ src/com/companyname/modulename/App.java
You can also specify a target class-file dir:
-d target/
.我只是将 make 与一个简单的 makefile 一起使用,如下所示:
它一次编译一个源代码,并且仅在必要时重新编译。
I'm just using make with a simple makefile that looks like this:
It compiles the sources one at a time and only recompiles if necessary.
我建议您学习使用 ant,它非常适合这项任务,并且非常容易掌握和使用有据可查。
您只需在 build.xml 文件中定义如下目标:
I would advice you to learn using ant, which is very-well suited for this task and is very easy to grasp and well documented.
You would just have to define a target like this in the build.xml file:
javac 命令不遵循递归编译过程,因此您可以在运行命令时指定每个目录,或者提供一个文本文件,其中包含要包含的目录:
javac command does not follow a recursive compilation process, so you have either specify each directory when running command, or provide a text file with directories you want to include:
这个很酷:
我位于项目根目录。
this one is pretty cool:
I am located at the project root.
我一直在 Xcode JNI 项目中使用它来递归构建我的测试类:
I've been using this in an Xcode JNI project to recursively build my test classes:
我肯定会在我的所有项目中使用 Maven(或类似的)。但是,一旦需要仅使用
javac
进行编译,我遇到了以下 Windows BAT 脚本:很可能它需要在细节上进行一些自定义。缺点是它不进行增量编译并每次都重新编译所有内容,但这正是我现在所需要的。
I would definitely use Maven (or the likes) in all my projects. But once needing a compilation with only
javac
I came across with the following Windows BAT script:Most likely it needs some customization in the details. The drawback is that it doesn't do incremental compilation and recompiles everything each time, but this is what I need for now.