javac 选项以递归方式编译给定目录下的所有 java 文件 [英] javac option to compile all java files under a given directory recursively

查看:35
本文介绍了javac 选项以递归方式编译给定目录下的所有 java 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 javac 编译器来编译我的项目中的 java 文件.这些文件分布在几个这样的包中:com.vistas.utilcom.vistas.convertercom.vistas.LineHelper、<代码>com.current.mdcontect.

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.

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

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

(一行)

我如何让编译器递归编译父 com 目录中的所有 java 文件,而不是给出这么多路径?

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

推荐答案

我还建议使用某种构建工具 (AntMaven、Ant 已经被推荐并且更容易开始)或处理编译的 IDE(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).

如果你需要为一个更大的项目尝试一些东西并且附近没有任何合适的构建工具,你总是可以使用 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 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.

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

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

  • 优点是一种快速简便的解决方案.
  • 缺点是每次创建新源或重命名现有文件时都必须重新生成 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.
    • 从长远来看,最好使用专为构建软件而设计的工具.

      On the long run it is better to use a tool that was designed to build software.

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

      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
      

      • 优点是您使用的是易于扩展的标准构建工具.
      • 缺点是您必须下载、设置和学习其他工具.请注意,大多数 IDE(如 NetBeans 和 Eclipse)都为编写构建文件提供了强大的支持,因此在这种情况下您无需下载任何内容.
        • 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.
        • Maven 的设置和使用并不是那么简单,但学习它是值得的.这是一个很棒的教程,可以在 5 分钟内启动一个项目.

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

          现在可以提高您的开发效率.有一些开源替代方案(例如 EclipseNetBeans,我更喜欢前者)甚至商业的(比如 IntelliJ) 非常流行且功能强大.

          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.

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

          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.

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

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

          这篇关于javac 选项以递归方式编译给定目录下的所有 java 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆