javac 选项递归编译给定目录下的所有 java 文件

发布于 2024-11-18 20:17:59 字数 478 浏览 2 评论 0 原文

我正在使用 javac 编译器来编译项目中的 java 文件。这些文件分布在多个包中,如下所示:com.vistas.utilcom.vistas.convertercom.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文件,而不是给出这么多路径?

I am using the javac compiler to compile java files in my project. The files are distributed over several packages like this: com.vistas.util, com.vistas.converter, com.vistas.LineHelper, com.current.mdcontect.

Each of these packages has several java files. I am using javac like this:

javac com/vistas/util/*.java com/vistas/converter/*.java
      com.vistas.LineHelper/*.java com/current/mdcontect/*.java

(in one line)

Instead of giving so many paths, how can I ask the compiler to compile recursively all the java files from the parent com directory?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

野鹿林 2024-11-25 20:17:59

我还建议使用某种构建工具(AntMaven,已经建议使用 Ant,并且更容易上手)或一个处理编译的 IDE(Eclipse 使用带有协调策略的增量编译,您甚至不必按任何“编译”按钮)。

使用 Javac

如果您需要尝试较大的项目,并且附近没有任何合适的构建工具,您始终可以使用 javac 提供的一个小技巧:可以在以下位置指定要编译的类名一个文件。您只需将文件名以 @ 前缀传递给 javac 即可。

如果您可以创建项目中所有 *.java 文件的列表,那就很简单:

# Linux / MacOS
$ find -name "*.java" > sources.txt
$ javac @sources.txt

:: Windows
> dir /s /B *.java > sources.txt
> javac @sources.txt
  • 优点是这是一个快速且简单的解决方案。
  • 缺点是,每次创建新源或重命名现有文件时,都必须重新生成 sources.txt 文件,这很容易忘记(因此会出现错误 -容易)和令人厌烦的任务。

使用构建工具

从长远来看,最好使用专为构建软件而设计的工具。

使用 Ant

如果您创建一个简单的 build.xml 文件来描述如何构建软件:

<project default="compile">
    <target name="compile">
        <mkdir dir="bin"/>
        <javac srcdir="src" destdir="bin"/>
    </target>
</project>

您可以通过运行以下命令来编译整个软件:

$ ant
  • 优点是您使用易于扩展的标准构建工具。
  • 缺点是您必须下载、设置和学习额外的工具。请注意,大多数 IDE(例如 NetBeans 和 Eclipse)都为编写构建文件提供了强大的支持,因此在这种情况下您无需下载任何内容。

使用 Maven

Maven 的设置和使用并不是那么简单,但学习它会带来丰厚的回报。这是一个很棒的教程,可以在 5 分钟内启动一个项目

  • 它的主要优点(对我来说)是它也可以处理依赖项,因此您不需要下载更多 Jar 文件并手动管理它们,我发现它对于构建、打包和测试更有用更大的项目。
  • 缺点是它有一个陡峭的学习曲线,如果 Maven 插件喜欢抑制错误:-) 另一件事是很多工具也可以使用 Maven 存储库(例如 Sbt 用于 Scala,常春藤对于蚂蚁来说, Graddle for Groovy)。

使用 IDE

现在什么可以提高您的开发效率。有一些开源替代方案(例如 EclipseNetBeans,我更喜欢前者),甚至商业的(例如 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 to javac with the @ prefix.

If you can create a list of all the *.java files in your project, it's easy:

# Linux / MacOS
$ find -name "*.java" > sources.txt
$ javac @sources.txt

:: Windows
> dir /s /B *.java > sources.txt
> javac @sources.txt
  • The advantage is that is is a quick and easy solution.
  • The drawback is that you have to regenerate the 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:

<project default="compile">
    <target name="compile">
        <mkdir dir="bin"/>
        <javac srcdir="src" destdir="bin"/>
    </target>
</project>

you can compile the whole software by running the following command:

$ ant
  • The advantage is that you are using a standard build tool that is easy to extend.
  • The drawback is that you have to download, set up and learn an additional tool. Note that most of the IDEs (like NetBeans and Eclipse) offer great support for writing build files so you don't have to download anything in this case.

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.

  • It's main advantage (for me) is that it handles dependencies too, so you won't need to download any more Jar files and manage them by hand and I found it more useful for building, packaging and testing larger projects.
  • The drawback is that it has a steep learning curve, and if Maven plugins like to suppress errors :-) Another thing is that quite a lot of tools also operate with Maven repositories (like Sbt for Scala, Ivy for Ant, Graddle for Groovy).

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 :-)

[旋木] 2024-11-25 20:17:59
find . -name "*.java" -print | xargs javac 

有点残酷,但工作起来却像地狱一样。 (仅在小程序上使用,效率绝对不高)

find . -name "*.java" -print | xargs javac 

Kinda brutal, but works like hell. (Use only on small programs, it's absolutely not efficient)

治碍 2024-11-25 20:17:59

在通常情况下,如果您想要编译整个项目,您可以简单地为主类提供 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

花辞树 2024-11-25 20:17:59

如果你的 shell 支持它,这样的东西会起作用吗?

javac com/**/*.java 

如果您的 shell 不支持 **,那么也许可以

javac com/*/*/*.java

工作(对于具有 3 个组件的所有软件包 - 或多或少进行调整)。

If your shell supports it, would something like this work ?

javac com/**/*.java 

If your shell does not support **, then maybe

javac com/*/*/*.java

works (for all packages with 3 components - adapt for more or less).

隔岸观火 2024-11-25 20:17:59

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/.

放赐 2024-11-25 20:17:59

我只是将 make 与一个简单的 makefile 一起使用,如下所示:

JAVAC = javac -Xlint:unchecked
sources = $(shell find . -type f -name '*.java')
classes = $(sources:.java=.class)

all : $(classes)

clean :
        rm -f $(classes)

%.class : %.java
        $(JAVAC) 
lt;

它一次编译一个源代码,并且仅在必要时重新编译。

I'm just using make with a simple makefile that looks like this:

JAVAC = javac -Xlint:unchecked
sources = $(shell find . -type f -name '*.java')
classes = $(sources:.java=.class)

all : $(classes)

clean :
        rm -f $(classes)

%.class : %.java
        $(JAVAC) 
lt;

It compiles the sources one at a time and only recompiles if necessary.

我纯我任性 2024-11-25 20:17:59

我建议您学习使用 ant,它非常适合这项任务,并且非常容易掌握和使用有据可查。

您只需在 build.xml 文件中定义如下目标:

<target name="compile">
    <javac srcdir="your/source/directory"
           destdir="your/output/directory"
           classpath="xyz.jar" />
</target>

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:

<target name="compile">
    <javac srcdir="your/source/directory"
           destdir="your/output/directory"
           classpath="xyz.jar" />
</target>
强者自强 2024-11-25 20:17:59

javac 命令不遵循递归编译过程,因此您可以在运行命令时指定每个目录,或者提供一个文本文件,其中包含要包含的目录:

javac -classpath "${CLASSPATH}" @java_sources.txt

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:

javac -classpath "${CLASSPATH}" @java_sources.txt
初心 2024-11-25 20:17:59

这个很酷:
我位于项目根目录。

find . -name *.java -exec javac {} -d ../out \;

this one is pretty cool:
I am located at the project root.

find . -name *.java -exec javac {} -d ../out \;
倾听心声的旋律 2024-11-25 20:17:59

我一直在 Xcode JNI 项目中使用它来递归构建我的测试类:

find ${PROJECT_DIR} -name "*.java" -print | xargs javac -g -classpath ${BUILT_PRODUCTS_DIR} -d ${BUILT_PRODUCTS_DIR}

I've been using this in an Xcode JNI project to recursively build my test classes:

find ${PROJECT_DIR} -name "*.java" -print | xargs javac -g -classpath ${BUILT_PRODUCTS_DIR} -d ${BUILT_PRODUCTS_DIR}
烟沫凡尘 2024-11-25 20:17:59

我肯定会在我的所有项目中使用 Maven(或类似的)。但是,一旦需要仅使用 javac 进行编译,我遇到了以下 Windows BAT 脚本:

@echo off
setlocal enabledelayedexpansion

rmdir /s /q target
rmdir /s /q generated
mkdir target
mkdir generated

set JAVA_FILES=

for /f "delims=" %%i in ('dir /a-d /s /b src\*.java') do (
    set "JAVA_FILES=!JAVA_FILES! "%%i""
)

javac -d target -encoding UTF-8 -classpath src; -sourcepath src; -s generated %JAVA_FILES%

endlocal

很可能它需要在细节上进行一些自定义。缺点是它不进行增量编译并每次都重新编译所有内容,但这正是我现在所需要的。

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:

@echo off
setlocal enabledelayedexpansion

rmdir /s /q target
rmdir /s /q generated
mkdir target
mkdir generated

set JAVA_FILES=

for /f "delims=" %%i in ('dir /a-d /s /b src\*.java') do (
    set "JAVA_FILES=!JAVA_FILES! "%%i""
)

javac -d target -encoding UTF-8 -classpath src; -sourcepath src; -s generated %JAVA_FILES%

endlocal

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文