You know the drill. Write code, compile it, run it. Write code, compile it, run it. Over and over. What if you could skip the middle step entirely?
Since JDK 11, Java lets you run a source file directly — no separate javac step needed. The java launcher handles compilation behind the scenes. And as of JDK 22, this works with multi-file programs too.
Let me walk through how it works, when to use it, and where it breaks down.
Single-File Source-Code Programs
Create a file called HelloWorld.java:
public class HelloWorld {
public static void main(String[] args) {
IO.println("Hello World!");
}
}
Instead of running javac HelloWorld.java followed by java HelloWorld, you just run:
$ java HelloWorld.java
That's it. The launcher compiles the file in memory and runs it. No .class file gets written to disk.
This works because the java launcher automatically invokes the Java compiler when you pass it a .java file. The compiled bytecode lives in memory for the duration of the program.
Passing Arguments
You can pass arguments the same way you would with a compiled class:
public class HelloJava {
public static void main(String[] args) {
IO.println("Hello " + args[0]);
}
}
$ java HelloJava.java World!
Output:
Hello World!
Nothing special here — the launcher forwards arguments to your main method just like normal.
Multiple Classes in One File
Sometimes you want a helper class alongside your main class. That works fine:
public class MultipleClassesInSameFile {
public static void main(String[] args) {
IO.println(GenerateMessage.generateMessage());
IO.println(AnotherMessage.generateAnotherMessage());
}
}
class GenerateMessage {
static String generateMessage() {
return "Here is one message";
}
}
class AnotherMessage {
static String generateAnotherMessage() {
return "Here is another message";
}
}
$ java MultipleClassesInSameFile.java
Output:
Here is one message
Here is another message
The first class in the file must have the public static void main method. The other classes can be package-private.
Multi-File Programs (JDK 22+)
JDK 22 added
SOCIAL SHARE CARD GENERATOR