00:00

Launch Single-File Source-Code Programs in Java 11

Last updated: · Standard (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

Commands
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

Greeter.java
// 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.");
    }
}
Output
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.

WordCount.java
// 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 { }
}
Output
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.

today (shebang script)
#!/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

Source-file mode steps The java launcher reads the source file, compiles it in memory with the compiler, loads the first top-level class with a special class loader, and calls its main method. java Greeter.java source-file mode compile in memory no .class written load first class memory class loader run main() with the arguments
Figure 1: The whole program is compiled and run in one launcher process.

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 .class file 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

7. References