Compiling Multiple Java Files Using Javac + Console Redirection

javac compiling multiple files console redirection

Using the Java Langage Compiler (javac) to compile .java files from the command line is super simple. Even though javac comes with a large list of possible arguments to specify use, compiling multiple java files isn’t easily achieved without outside help—unless you want to type them all out as cmd arguments each time!

Example Project structure

To illustrate how to compile multiple files in a Java project using only the command prompt, javac, and the file command (unix/linux), we’ll be working with the following example project directory structure:

.
└── ExampleProject/
    └── src/
        ├── ClassOne.java
        ├── ClassTwo.java
        └── ClassThree.java

Step 1: Create a List of All Source Files

Javac accepts an argument for a file containing all command-line arguments. This functionality opens the door for all kinds of possibilities but for the context of this article, we’ll be using it to load all source files for compilation.

On linux/Unix machine, the following command (issued from the ExampleProject directory) will search for all java source files and save them into a file names sources.txt:

find -name "*.java" > sources.txt

This command results in the creation of a new sources.txt file in our ExampleProject directory which contains the following information:

./src/ClassOne.java
./src/ClassTwo.java
./src/ClassThree.java

Step 2: Compile Source Files

After the sources.txt file has been generated one need only rely on the default functionality of the javac compiler, by specifying that command-line arguments be read from a file. In this case, multiple source files will be read in.

javac @sources.txt

After this command, the project structure will look like the following:

└── ExampleProject/
    ├── sources.txt
    └── src/
        ├── ClassOne.java
        ├── ClassTwo.java
        ├── ClassThree.java
        ├── ClassOne.class
        ├── ClassTwo.class
        └── ClassThree.class

Pros vs. Cons

The benefits of this method of compiling multiple files with javac are that it has no dependencies and is super fast. However, the major drawback here is that the sources.txt file needs to be regenerated every time a new source file is created or renamed.

Build Tool Options

Being that Java is a mature, enterprise-grade, well-supported programming language—there’s plenty of automated build tools that can help with compilation. The most popular are as follows:

These stand-alone tools provide an array of automated workflow support including compilation automation. Aside from these, many modern Java IDEs like IntelliJ or Eclipse feature built-in compilation automation packages that help built project files in the background.

Zαck West
Full-Stack Software Engineer with 10+ years of experience. Expertise in developing distributed systems, implementing object-oriented models with a focus on semantic clarity, driving development with TDD, enhancing interfaces through thoughtful visual design, and developing deep learning agents.