Java: How can I compile an entire directory structure of code ?

JavaCompilationJavacDirectory Structure

Java Problem Overview


The use case is simple. I got the source files that were created using Eclipse. So, there is a deep directory structure, where any Java class could be referring to another Java class in the same, child, sibling or parent folder.

How do I compile this whole thing from the terminal using javac ?

Java Solutions


Solution 1 - Java

You have to know all the directories, or be able to use wildcard ..

javac dir1/*.java dir2/*.java dir3/dir4/*.java dir3/dir5/*.java dir6/*src/*.java

Solution 2 - Java

With Bash 4+, you can just enable globstar

shopt -s globstar

and then do

javac **/*.java

Solution 3 - Java

If all you want to do is run your main class (without compiling the .java files on which the main class doesn't depend), then you can do the following:

cd <root-package-directory>
javac <complete-path-to-main-class>

or

javac -cp <root-package-directory> <complete-path-to-main-class>

javac would automatically resolve all the dependencies and compile all the dependencies as well.

Solution 4 - Java

I would take Jon's suggestion and use Ant, since this is a pretty complex task.

However, if you are determined to get it all in one line in the Terminal, on Linux you could use the find command. But I don't recommend this at all, since there's no guarantee that, say, Foo.java will be compiled after Bar.java, even though Foo uses Bar. An example would be:

find . -type f -name "*.java" -exec javac {} \;

If all of your classes haven't been compiled yet, if there's one main harness or driver class (basically the one containing your main method), compiling that main class individually should compile most of project, even if they are in different folders, since Javac will try to the best of its abilities to resolve dependency issues.

Solution 5 - Java

Following is the method I found:

  1. Make a list of files with relative paths in a file (say FilesList.txt) as follows (either space separated or line separated):

    foo/AccessTestInterface.java foo/goo/AccessTestInterfaceImpl.java

  2. Use the command:

    javac @FilesList.txt -d classes This will compile all the files and put the class files inside classes directory.

Now easy way to create FilesList.txt is this: Go to your source root directory.

dir *.java /s /b > FilesList.txt

But, this will populate absolute path. Using a text editor "Replace All" the path up to source directory (include \ in the end) with "" (i.e. empty string) and Save.

Solution 6 - Java

The already existing answers seem to only concern oneself with the *.java files themselves and not how to easily do it with library files that might be needed for the build.

A nice one-line situation which recursively gets all *.java files as well as includes *.jar files necessary for building is:

javac -cp ".:lib/*" -d bin $(find ./src/* | grep .java)

Here the bin file is the destination of class files, lib (and potentially the current working directory) contain the library files and all the java files in the src directory and beneath are compiled.

Solution 7 - Java

You'd have to use something like Ant to do this hierarchically:

http://ant.apache.org/manual/Tasks/javac.html

You'll need to create a build script with a target called compile containing the following:

<javac sourcepath="" srcdir="${src}"
         destdir="${build}" >
    <include name="**/*.java"/>
</javac>

Then you''ll be able to compile all files by running:

 ant compile

Alternatively, import your project into Eclipse and it will automatically compile all the source files for that project.

Solution 8 - Java

There is a way to do this without using a pipe character, which is convenient if you are forking a process from another programming language to do this:

find $JAVA_SRC_DIR -name '*.java' -exec javac -d $OUTPUT_DIR {} +

Though if you are in Bash and/or don't mind using a pipe, then you can do:

find $JAVA_SRC_DIR -name '*.java' | xargs javac -d $OUTPUT_DIR

Solution 9 - Java

Windows solution: Assuming all files contained in sub-directory 'src', and you want to compile them to 'bin'.

for /r src %i in (*.java) do javac %i -sourcepath src -d bin

If src contains a .java file immediately below it then this is faster

javac src\\*.java -d bin

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questioneuphoria83View Question on Stackoverflow
Solution 1 - JavaManidip SenguptaView Answer on Stackoverflow
Solution 2 - JavaDaniel LubarovView Answer on Stackoverflow
Solution 3 - JavaBharat KhatriView Answer on Stackoverflow
Solution 4 - JavaZach LView Answer on Stackoverflow
Solution 5 - JavaAmeya GokhaleView Answer on Stackoverflow
Solution 6 - JavademongolemView Answer on Stackoverflow
Solution 7 - JavaJonView Answer on Stackoverflow
Solution 8 - JavabolinfestView Answer on Stackoverflow
Solution 9 - JavaMikeView Answer on Stackoverflow