Launching Single -File Source-Code Programs
Launching Single -File Source-Code Programs
In JDK 11, Java introduced the ability to launch a single-file source-code program with the java launcher,
without first needing to explicitly compile the source code. This works by the java launcher automatically
invoking the compiler and storing the compiled code in-memory. This can be a great way to learn how to
use Java or explore new features within the Java API, without having to go through the cruft of compiling
and then executing code. There are several ways to use this feature, as well as some limitations and
things to keep in mind.
To execute a single-file source-code program, the first class defined in the source file must contain public
static void main(String[]) like in HelloWorld below:
System.out.println("Hello World!");
Copy
From the command line, HelloWorld can be launched with (accepting the name of the file is
also HelloWorld.java):
$ java HelloWorld.java
Copy
Passing in Arguments
Arguments can also be passed in like with a normally compiled class, so in the below:
Copy
Copy
Multiple classes can be defined within the same source file if needed for encapsulation purposes, like in
this example:
System.out.println(GenerateMessage.generateMessage());
System.out.println(AnotherMessage.generateAnotherMessage());
class GenerateMessage {
class AnotherMessage {
}
}
Copy
When executed:
$ java MultipleClassesInSameFile.java
Copy
Will output:
Copy
A class that is part of the core JDK does not need to be added to the classpath to be executed. So this
example, referencing the Scanner and MatchResult classes, can be executed simply with
the java launcher:
import java.util.Scanner;
import java.util.regex.MatchResult;
daybreak 17 benign
9 homecoming 1
freight car
""";
scanner.findAll("benign").map(MatchResult::group).forEach(System.out::println);
}
Copy
To launch:
$ java ScannerExample.java
Copy
However the below example referencing RandomUtils, part of the Apache Commons Lang, will need to
have the commons-lang.jar added to the classpath at launch:
import org.apache.commons.lang3.RandomUtils;
System.out.println(RandomUtils.nextInt());
Copy
To launch:
Copy
On a Unix-like operating system, a single-file source-code application, can also be launched as a shebang
file like a script. Within the a java source file, as the first line in the file add path/to/java/home --source
<version> like in the below example:
#!/path/to/your/bin/java --source 23
Copy
The file cannot have .java as its file extension, and must also be executable chmod +x. With that, it can
be launched with:
./HelloJava