1. Overview
In some special circumstances, we don’t have a third-party build tool installed, such as Ant or Maven. And, we still need to compile a project with lots of packages and classes.
In this tutorial, we’re going to use the javac command to accomplish this task with different scenarios.
2. Using File Names
Let’s assume we have two directories in the current directory: src and out. The src directory holds our Java source files, and the out directory will contain the corresponding compiled class files.
Let’s start with a simple scenario. The src directory contains a single Java source file named com/baeldung/MyClass.java:
Then, let’s use javac to compile the MyClass.java file into the out directory:
$ javac -d ./out/ ./src/com/baeldung/MyClass.java
In the above command, the -d option specifies the destination directory for the class file. Also, we should note that the exact code of the MyClass.java file isn’t that important, and we only need to ensure it’s a grammar-correct Java file.
To be a little complicated, let’s add another three Java files – YourClass.java, HerClass.java, and HisClass.java:
To compile all four of the above Java files, we can list each of them on the command line:
$ javac -d ./out/ \
./src/com/baeldung/MyClass.java \
./src/com/baeldung/YourClass.java \
./src/com/baeldung/HerClass.java \
./src/com/baeldung/HisClass.java
Then, let’s add a new Main.java file that refers to the other four Java files, for example, by invoking methods or creating an object instance:
In this case, we only need to compile the Main.java file:
$ javac -sourcepath ./src/ -d ./out/ ./src/com/baeldung/Main.java
After the above command, the other four class files will also be compiled. That’s because javac will search for the required types and compile the corresponding source files by default. If we don’t want to compile the required types, we can add the -implicit:none option.
The -sourcepath option tells the Java compiler where to find the input source files. If the -sourcepath option isn’t specified, javac will utilize the user classpath to search for both the class files and source files. So, we can replace the -sourcepath option with the -classpath or -cp option:
$ javac -cp ./src/ -d ./out/ ./src/com/baeldung/Main.java
However, this approach has its limitations: the javac command only compiles the required types and omits other source files. For example, if we add a new ItsClass.java and the Main.java doesn’t refer to it, then ItsClass.java will not be compiled:
To summarize, there are two scenarios suitable to list filenames in the javac command line: when there are just a few Java source files, and when there is a launcher class that refers to other classes recursively.
3. Using Wildcard
The javac command also supports the wildcard character (*) for compiling multiple source files in the same directory.
For example, we can use the wildcard to compile the above source files:
$ javac -d ./out/ ./src/com/baeldung/*.java
To complicate our scenario further, let’s add four sub-packages (spring, summer, autumn, and winter) and corresponding classes:
Now, in the command line, we can list each package with a wildcard to compile all of them:
$ javac -d ./out/ \
./src/com/baeldung/*.java \
./src/com/baeldung/spring/*.java \
./src/com/baeldung/summer/*.java \
./src/com/baeldung/autumn/*.java \
./src/com/baeldung/winter/*.java
When there are only a few packages, regardless of the source file number, it’s suitable to use this wildcard approach.
4. Using Argument Files
When there are multiple packages to compile, then using the javac command with an argument file comes in handy. An argument file can include both the javac options and source file names.
To use an argument file, we need to prepend the at sign (@) leading character before the argument filename:
$ javac -d ./out/ @sources.txt
But how can we generate such a @sources.txt file? That depends on the OS we’re using. In Linux or macOS, we can use the find command:
$ find ./src/ -type f -name "*.java" > sources.txt
In the above command line, the ./src/ is our search starting-point directory, the -type f option filters only regular files, and the -name “*.java” option matches all filenames with the .java extension.
However, in Windows, we can use the dir command:
> dir src /b /s *.java > sources.txt
In the above command line, the src folder is our search path, the /b switch shows the directory and file names without additional information, and the /s option lists all files in a specified directory and all subdirectories.
The shortcoming of this approach is that each time we add a new or remove an existing Java source file, we need to regenerate the sources.txt file.
5. Other Approaches
Besides the above common approaches, there also exist other OS-dependent approaches, such as using globstar or pipe.
5.1. Using Globstar
Bash version 4.0 adds a new globbing option called globstar that treats the double wildcard (**) differently. With it enabled, Bash will traverse multi-level directories; otherwise, Bash will only search a single-level directory.
However, this option is disabled by default. And, we can use the shopt (sh + opt, shell options) command to inspect the Bash option settings. If we execute the shopt command without any argument, it will output a long list of options and their statuses (on or off).
Currently, we’re only concerned with the globstar option:
$ shopt globstar
globstar off
To enable it, we use the shopt command with the -s option:
$ shopt -s globstar
To disable it, we invoke the shopt command with the -u option:
$ shopt -u globstar
After enabling this option, we can invoke javac with the double wildcard:
$ javac -d ./out/ ./src/**/*.java
5.2. Using a Pipe
Conceptually, a pipe is a connection between two processes. And, we can utilize this pipe mechanism to connect multiple commands to produce our desired results.
To compile our Java source files, we can combine the find, xargs, and javac commands:
$ find ./src/ -type f -name "*.java" | xargs javac -cp ./src/ -d ./out/
Also, the find command supports the -exec action:
$ find ./src/ -type f -name "*.java" -exec javac -cp ./src/ -d ./out/ '{}' ';'
The above command line may run a bit slowly. That’s because the javac command will run for each matched file. For more information, we can use the man find command to read the -exec option’s documentation.
To be a little faster, we can change the semicolon (;) into a plus sign (+). Then, the javac command will collect all the matched files and execute only once:
$ find ./src/ -type f -name "*.java" -exec javac -cp ./src/ -d ./out/ '{}' +
6. Conclusion
In this article, we first looked at some common approaches to compiling all Java source files in a directory structure, such as using filenames, wildcards, and an argument file. Then, we looked at some OS-dependent approaches, such as using globstar and pipe.