Launch Single-File Source-Code Programs in Java 11
Since Java 11 (JEP 330), the java launcher can run a program directly from a .java source file: java Hello.java. The file is compiled in memory and executed, without a separate javac step and without writing .class files.
This tutorial shows how to run a file with arguments, how to use the feature for small scripts, and what its limits are.
1. Why Was This Feature Added?
Running even "Hello, world" used to need two commands and produced class files you then had to clean up. That is a hurdle for beginners and for small utility programs. Source-file mode makes Java feel like a scripting language for quick tasks, while the code stays normal Java.
2. Syntax
java Greeter.java # run the first top-level class in the file
java Greeter.java Asha Ravi # arguments after the file name go to main()
java --source 11 Greeter.java # choose the language version
java -cp lib/json.jar Tool.java # the file can use libraries on the class path
3. Practical Examples
Example 1: Running a file with arguments
// Run directly from source (Java 11+): java Greeter.java Asha Ravi
import java.util.List;
public class Greeter {
public static void main(String[] args) {
List<String> names = args.length > 0 ? List.of(args) : List.of("world");
for (String name : names) {
System.out.println("Hello, " + name + "!");
}
System.out.println("Compiled in memory; no .class file was written.");
}
}
Hello, Asha! Hello, Ravi! Compiled in memory; no .class file was written.
Compile and run: java --source 11 Greeter.java Asha Ravi
Example 2: A small utility script
A source file may contain several classes. The launcher runs the main method of the first top-level class.
// A small utility script: java WordCount.java
import java.util.Arrays;
import java.util.Map;
import java.util.TreeMap;
public class WordCount {
public static void main(String[] args) {
String text = "to be or not to be that is the question";
Map<String, Integer> counts = new TreeMap<>();
Arrays.stream(text.split(" ")).forEach(w -> counts.merge(w, 1, Integer::sum));
counts.forEach((word, n) -> System.out.println(word + " = " + n));
}
// A source file may contain several classes; the first top-level class is launched
static class Helper { }
}
be = 2 is = 1 not = 1 or = 1 question = 1 that = 1 the = 1 to = 2
Compile and run: java --source 11 WordCount.java
Example 3: A shebang script on Linux and macOS
A file without the .java extension can start with a #! line and be executed directly once it is made executable with chmod +x. The --source option is required in that case.
#!/usr/bin/java --source 11
public class Today {
public static void main(String[] args) {
System.out.println(java.time.LocalDate.now());
}
}
4. What Happens When You Run java File.java
5. Important Points and Best Practices
- In Java 11 the program must fit in one source file. Java 22 (JEP 458) later allowed a program to use other source files.
- Compilation errors are reported and the program does not run.
- The file is compiled every time it runs, so this mode suits small programs and scripts, not large applications.
- If a matching
.classfile is on the class path, the launcher may report an error instead of running the source; keep scripts in their own folder.
6. Summary
- Java 11 (JEP 330) can run a program directly with
java File.java. - The source is compiled in memory; no class files are written.
- Arguments after the file name are passed to
main. - Shebang scripts work on Unix-like systems with
--source.
Related Tutorials
- Compact Source Files and Instance Main Methods (Java 25)
- Java 11 Features
- Local Variable Syntax for Lambda Parameters
- Java Introduction
7. References
Frequently Asked Questions (FAQ)
Q1. Can Java run a .java file without compiling it first?
Since Java 11 you can run java File.java. The launcher still compiles the file, but in memory and automatically.
Q2. Which class is run in source-file mode?
The first top-level class declared in the file; it must have a standard main method.
Q3. Does source-file mode create .class files?
No. The compiled classes are kept in memory and discarded when the program ends.
Q4. Can the single file use external libraries?
Yes. Put the libraries on the class path, for example java -cp lib/gson.jar Tool.java.