Вы находитесь на странице: 1из 4

javac - Compiling java files in all subfolders?

- Stack Overflow Page 1 of 4

Stack Overflow is a community of 7.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up

Compiling java files in all subfolders? [duplicate]

This question already has an answer here:


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

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

java javac

edited Dec 16 '13 at 10:39 asked Mar 4 '11 at 14:19


Danubian Sailor Patrick
15.8k 23 109 175 16.5k 77 226 425

marked as duplicate by Danubian Sailor, Matsemann, Adam Arold, Dmitry Pashkevich, Felipe Oriani Dec 16 '13 at 11:27
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

6 Answers

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>

https://stackoverflow.com/questions/5194926/compiling-java-files-in-all-subfolders 9/21/2017
javac - Compiling java files in all subfolders? - Stack Overflow Page 2 of 4

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

edited Mar 4 '11 at 14:30 answered Mar 4 '11 at 14:23


Kim Burgaard
2,652 9 11

1 Shouldn't the right answer be: "you do it like this... but Ant/Maven will work out better for you"? Someone
who's actually trying to grasp how to use javac (what the question is aimed at) will not find an answer in this.
dmg_ Jun 25 '16 at 17:58

On Windows...

Firstly create a batch file as complie.bat. Write these lines in that

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

now execute.

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.

edited Dec 29 '11 at 7:52 answered Mar 4 '11 at 14:20


Community Orbit
1 1 11.3k 2 26 56

thanks. I get this error message though: javac: invalid flag: ./lucene/org/apache/lucene/.svn/prop-
base/LucenePackage.java.svn-base Patrick Mar 4 '11 at 14:28

oops, seems that file has a .java extension and you don't want to compile it, does it work with grep
--include=.java? did it still compile your other files? Orbit Mar 4 '11 at 14:32

for /r %a in (*.java) do (jikes.exe -d bin\ -cp jre6\lib\rt.jar;WEB-INF\lib\*.jar "%a") diyism Nov 13 '11 at 7:27

for /r %a in (*.java) do (java -jar ecj.jar -d bin\ -cp jre6\lib\rt.jar;WEB-INF\lib*.jar "%a") diyism Nov 13 '11 at
12:47

Try grep "java$" chronospoon Dec 13 '13 at 20:41

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

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

edited Mar 4 '11 at 14:31 answered Mar 4 '11 at 14:22


krtek
21.2k 4 41 73

uhm, it doesn't work. Is maybe because find command works differently on mac ? (-name flag doesn't exist)
Patrick Mar 4 '11 at 14:27

1 according to developer.apple.com/library/mac/#documentation/Darwin/Reference/ the -name option


exists. krtek Mar 4 '11 at 14:29

Even if -name is supported, it won't work if a .java file exists in the current folder, since the shell will expand
the wildcard. Use quotes (aka find -name "*.java"). Axel Mar 4 '11 at 14:31

I modified the answer to add the quotes and the path to use. It should work on Mac OS X this time. And it
works on Debian without the quotes : $ find . -name *.java ---> ./test.java krtek Mar 4 '11 at 14:32

https://stackoverflow.com/questions/5194926/compiling-java-files-in-all-subfolders 9/21/2017
javac - Compiling java files in all subfolders? - Stack Overflow Page 3 of 4

Use Ant to write a script to compile as many source folders as


you want.

answered Mar 4 '11 at 14:22


Andrey Adamovich
13.4k 10 71 114

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)

answered Mar 4 '11 at 14:24


Sean Patrick Floyd
197k 37 352 488

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.

answered Mar 4 '11 at 15:17


Palo Ebermann
53.9k 11 104 169

https://stackoverflow.com/questions/5194926/compiling-java-files-in-all-subfolders 9/21/2017
javac - Compiling java files in all subfolders? - Stack Overflow Page 4 of 4

https://stackoverflow.com/questions/5194926/compiling-java-files-in-all-subfolders 9/21/2017

Вам также может понравиться