HOWTO · Java
How to Compile Multiple Java Files Using a Single Command in Java
This article introduces how to compile multiple java files using a single command in Java.
On this page
Use javac to compile one or more Java source files. You can name each file explicitly, or use *.java to select matching files in a directory. Compilation produces .class files; run a class separately with the java command. The examples below assume the files are in your current working directory unless a path is shown.
Compile a Java File
Start with one source file to see the compile-and-run steps. Save this complete program as Hello.java:
Run this example:
class Hello {
public static void main(String[] args) {
System.out.println("Hello");
}
}
Hello
Compile it from the directory containing Hello.java with javac Hello.java. The compiler writes Hello.class in that directory by default. To run the program, use java Hello from the same directory. The run command takes the class name, without the .class extension; compiling alone does not execute main.
Compile Multiple Java Files
To compile several source files in one invocation, list every filename after javac, separated by spaces. For example, save the Hello class above in Hello.java and a second class in Hello2.java:
class Hello2 {
public static void main(String[] args) {
System.out.println("Hello from 2");
}
}
Hello from 2
Compile both with javac Hello.java Hello2.java. This asks the compiler to process those two named source files together and, by default, creates Hello.class and Hello2.class beside them. The two filenames are the source files you explicitly pass to javac; they do not necessarily define every source file compiled. When a named source file refers to a class whose source is available and no matching class file is found, javac can locate and compile that dependency implicitly. Add another filename when you want to pass that source file explicitly, or when source lookup will not find it automatically.
The directory from which you run the command matters for relative filenames: javac Hello.java Hello2.java looks for those paths relative to the shell’s current working directory. Change to the directory containing the files first, or provide paths to the source files. Keep the filenames in the command consistent with the names on disk, including capitalization on systems where file paths are case-sensitive.
Compile All Java Files in a Directory
When the current directory contains several Java files that should be compiled, use javac *.java. The *.java pattern selects names ending in .java in that directory. Expansion is handled by the command environment, so it is a convenient way to pass matching source files without typing each name. It applies to the current directory only; it does not search child directories recursively. Use it when the files in that directory belong in the same compilation.
To select Java files in another directory, put the directory path before the wildcard. For example, javac /path/to/directory/*.java illustrates the form: replace /path/to/directory with the actual directory. The command’s working directory still determines how a relative path is interpreted. If a path contains spaces, quote the path portion according to your shell’s syntax; avoid adding spaces around path separators or the wildcard. The wildcard selects source filenames, while javac compiles them and writes class files to the default output location unless you specify a different destination.