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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6623161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 08:20:36  来源:igfitidea点击:

javac option to compile all java files under a given directory recursively

javajavac

提问by user496934

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.

我正在使用 javac 编译器在我的项目中编译 java 文件。这些文件分布在多个包中,如下所示:com.vistas.utilcom.vistas.convertercom.vistas.LineHelpercom.current.mdcontect

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

这些包中的每一个都有几个 java 文件。我正在像这样使用 javac:

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?

与其提供这么多路径,不如让编译器递归编译父 com 目录中的所有 java 文件?

采纳答案by rlegendi

I would also suggest using some kind of build tool (Antor 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).

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

Using Javac

使用 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 javacoffers: the classnames to compile can be specified in a file. You simply have to pass the name of the file to javacwith the @prefix.

如果你需要为一个更大的项目尝试一些东西并且附近没有任何合适的构建工具,你总是可以使用一个小技巧javac:可以在文件中指定要编译的类名。您只需将文件名传递到javac@前缀。

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

如果您可以创建*.java项目中所有文件的列表,则很容易:

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

:: Windows
> dir /s /B *.java > sources.txt
> javac @sources.txt
  • The advantageis that is is a quick and easy solution.
  • The drawbackis that you have to regenerate the sources.txtfile 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.
  • 优点是这是一个快速简便的解决方案。
  • 缺点sources.txt每次创建新源或重命名现有文件时都必须重新生成文件,这是一项容易忘记(因此容易出错)且令人厌烦的任务。

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.xmlfile that describes how to build the software:

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

<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 advantageis that you are using a standard build tool that is easy to extend.
  • The drawbackis 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.
  • 优点是您使用的是易于扩展的标准构建工具。
  • 缺点是您必须下载、设置和学习其他工具。请注意,大多数 IDE(如 NetBeans 和 Eclipse)都为编写构建文件提供了强大的支持,因此在这种情况下您无需下载任何内容。

Using Maven

使用 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.

Maven 的设置和使用并不是那么简单,但学习它是值得的。这是一个很棒的教程,可以在 5 分钟内开始一个项目

  • 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 drawbackis 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 Sbtfor Scala, Ivyfor Ant, Graddlefor Groovy).
  • 它的主要优点(对我而言)是它也处理依赖项,因此您无需下载更多 Jar 文件并手动管理它们,我发现它对于构建、打包和测试更大的项目更有用。
  • 缺点是它有一个陡峭的学习曲线,如果 Maven 插件喜欢抑制错误:-) 另一件事是,相当多的工具也使用 Maven 存储库(如Sbtfor Scala,Ivyfor Ant,Graddlefor Groovy) .

Using an IDE

使用 IDE

Now that what could boost your development productivity. There are a few open source alternatives (like Eclipseand NetBeans, I prefer the former) and even commercial ones (like IntelliJ) which are quite popular and powerful.

现在,什么可以提高您的开发生产力。有一些开源替代品(如EclipseNetBeans,我更喜欢前者)甚至商业替代品(如IntelliJ),它们非常流行且功能强大。

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 happensin the background so you can hunt down occasional errors like a ClassNotFoundException.

他们可以在后台管理项目构建,因此您不必处理所有命令行内容。但是,如果您知道后台实际发生了什么,那么它总是会派上用场的,这样您就可以找到诸如ClassNotFoundException.

One additional note

补充说明

For larger projects, it is always advised to use an IDE anda 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:eclipsecommand). 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 :-)

对于较大的项目,始终建议使用 IDE构建工具。前者可以提高您的工作效率,而后者可以在项目中使用不同的 IDE(例如,Maven 可以使用简单的mvn eclipse:eclipse命令生成 Eclipse 项目描述符)。此外,拥有可以使用单行命令测试/构建的项目很容易介绍给新同事和持续集成服务器。小菜一碟 :-)

回答by phtrivier

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

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

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

有效(适用于所有具有 3 个组件的包 - 或多或少地适应)。

回答by JB Nizet

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.

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

You would just have to define a target like this in the build.xml file:

你只需要在 build.xml 文件中定义一个这样的目标:

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

回答by Matthieu Riegler

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

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

有点残忍,但工作得像地狱。(只在小程序上使用,绝对效率不高)

回答by gvalenncia

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 命令不遵循递归编译过程,因此您可以在运行命令时指定每个目录,或者提供包含要包含的目录的文本文件:

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

回答by bishopthom

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

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

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

回答by Edward Doolittle

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

我只是将 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) $<

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

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

回答by freaker

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 与您的主类一起提供,并让它编译所有必需的依赖项:

javac -sourcepath . path/to/Main.java

javac -sourcepath . path/to/Main.java

回答by Curtis Yallop

javac -cp "jar_path/*" $(find . -name '*.java')

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)

(我不喜欢使用 xargs,因为它可以将它们拆分并多次运行 javac,每个都有一个 java 文件的子集,其中一些可能会导入未在同一 javac 命令行中指定的其他文件)

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:

如果您有 App.java 入口点,那么使用 -sourcepath 的 freaker 方法是最好的。它按照导入依赖项编译它需要的所有其他 java 文件。例如:

javac -cp "jar_path/*" -sourcepath src/ src/com/companyname/modulename/App.java

javac -cp "jar_path/*" -sourcepath src/ src/com/companyname/modulename/App.java

You can also specify a target class-file dir: -d target/.

您还可以指定目标类文件目录:-d target/.