编译所有子文件夹中的java文件?

ane*_*yzm 20 java javac

如何使用javac编译Unix上所有子文件夹中的所有java文件?

Orb*_*bit 27

在Windows上......

创建批处理文件:

for /r %%a in (.) do (javac %%a\*.java)
Run Code Online (Sandbox Code Playgroud)

...然后在顶级源文件夹中执行它.

在Linux上......

javac $(find ./rootdir/* | grep .java)
Run Code Online (Sandbox Code Playgroud)

两个答案取自这个线程......

http://forums.oracle.com/forums/thread.jspa?threadID=1518437&tstart=15

但正如其他人所说,构建工具可能会有所帮助.


Kim*_*ard 17

使用AntMaven等构建工具.两者都允许您以比使用例如findUNIX工具完成的更好的方式管理依赖项.And和Maven还允许您定义除编译之外要执行的自定义任务.Maven还提供了用于管理远程存储库中的外部依赖关系的约定,以及用于运行单元测试和支持持续集成的功能的约定.

即使您只是需要偶尔编译源文件,您可能会发现设置一个简单的Ant build.xml文件最终可以节省大量时间.

最后,大多数流行的IDE和代码编辑器应用程序都与Ant构建脚本进行了某种集成,因此您可以在编辑器中运行所有Ant任务.NetBeans,Eclipse,IDEA等也内置了对Maven的支持.

如果您是Ant新手,请先阅读此内容.以下是链接中的示例构建文件:

<project name="MyProject" default="dist" basedir=".">
    <description>
        simple example build file
    </description>
  <!-- set global properties for this build -->
  <property name="src" location="src"/>
  <property name="build" location="build"/>
  <property name="dist"  location="dist"/>

  <target name="init">
    <!-- Create the time stamp -->
    <tstamp/>
    <!-- Create the build directory structure used by compile -->
    <mkdir dir="${build}"/>
  </target>

  <target name="compile" depends="init"
        description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
    <javac srcdir="${src}" destdir="${build}"/>
  </target>

  <target name="dist" depends="compile"
        description="generate the distribution" >
    <!-- Create the distribution directory -->
    <mkdir dir="${dist}/lib"/>

    <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
    <jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/>
  </target>

  <target name="clean"
        description="clean up" >
    <!-- Delete the ${build} and ${dist} directory trees -->
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
  </target>
</project>
Run Code Online (Sandbox Code Playgroud)

一旦熟悉了Ant,就会发现移动到Maven更容易.

  • 不应该是正确的答案:"你这样做......但Ant/Maven会为你做得更好"?实际上试图掌握如何使用javac(问题针对的是什么)的人将无法找到答案. (3认同)

krt*_*tek 8

我不知道这是不是最好的方法,但这应该有效:

find . -name "*.java" | xargs javac
Run Code Online (Sandbox Code Playgroud)