Compiling java files in all subfolders? [duplicate

2019-01-06 17:53发布

This question already has an answer here:

How to compile all java files in all subfolders on Unix, using javac?

标签: java javac
6条回答
该账号已被封号
2楼-- · 2019-01-06 18:29

Use a build tool such as Ant or Maven. Both lets you manage dependencies in a much better way than can be accomplished using e.g. the find UNIX tool. Both And and Maven also lets you define custom tasks to be performed in addition to compilation. Maven furthermore comes with conventions for managing external dependencies in remote repositories, as well as conventions for running unit tests and features that support continuous integration.

Even if you just need to compile your source files once in a while, you'll probably find that setting up a simple Ant build.xml file can be a big time saver in the end.

Finally, most of the popular IDE and code editor applications has some kind of integration with Ant build scripts, so you can run all the Ant tasks from within the editor. NetBeans, Eclipse, IDEA and more also has built-in support for Maven.

Read this first, if you're new to Ant. Below is the example build file from the link:

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

Once you're familiar with Ant, you'll find it easier to move to Maven.

查看更多
Melony?
3楼-- · 2019-01-06 18:35

On Windows...

Create a batch file:

for /r %%a in (.) do (javac %%a\*.java)

...then execute it in the top-level source folder.

On Linux...

javac $(find ./rootdir/* | grep .java)

Both answers taken from this thread...

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

But as others suggested, a build tool would probably prove helpful.

查看更多
Anthone
4楼-- · 2019-01-06 18:36

I don't know if it is the best way, but this should work :

find . -name "*.java" | xargs javac
查看更多
兄弟一词,经得起流年.
5楼-- · 2019-01-06 18:43

Use Ant to write a script to compile as many source folders as you want.

查看更多
不美不萌又怎样
6楼-- · 2019-01-06 18:48

Another (less flexible) way, if you know how much folder levels there are:

javac *.java */*.java */*/*.java */*/*/*.java */*/*/*/*.java ...

Depending on your shell, you may have to set it to expanding non-matching patterns to nothing, in bash with shopt -s nullglob. For example, I'm using the following shell function to find text in my java files:

function jgrep ()
{
    (
      shopt -s nullglob
      egrep --color=ALWAYS -n "$@" *.tex *.java */*.java */*/*.java */*/*/*.java */*/*/*/*.java */*/*/*/*/*.java
    )
} 

jgrep String

But really, use an build tool, as the others said.

查看更多
狗以群分
7楼-- · 2019-01-06 18:50

Use Maven (as a more modern alternative to Ant).

Use an IDE, like Eclipse (all IDEs I know will happily compile multiple source folders for you)

查看更多
登录 后发表回答