The Single-File Source-Code Programs feature, introduced in Java 11 via JEP 330, allows developers to execute a Java source file directly using the java launcher without explicitly compiling it first. This feature is particularly useful for beginners learning Java, writing small utility programs, or quickly testing code snippets.
Important Points
- Purpose: Simplifies running single-file Java programs by eliminating the need for a separate compilation step (using javac).
- How It Works: The java launcher compiles the source file in memory (no .class file is generated on disk) and executes the first public static void main(String[] args) method found in the file.
- Command:
Â
java SourceFile.java [args...]
Replace SourceFile.java with the name of your Java source file.
Arguments after the file name are passed to the main method.
Code language: CSS (css)
Limitations:
- All code must reside in a single .java file. Multiple files require traditional compilation.
- External .class files (outside the JDK) cannot be referenced.
- The feature is designed for simple programs, not complex projects with dependencies.
Sample Implementation
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Code language: JavaScript (javascript)
Run it directly:
java HelloWorld.java
//Output
Hello, World!
Code language: JavaScript (javascript)
Additional Features
Shebang Support (Unix-like Systems):
- You can create executable scripts using a shebang line (e.g., #!/usr/bin/java –source 11).
- The file must not end in .java and must be executable (chmod +x).
- Example:
Â
#!/usr/bin/java --source 11
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, " + args[0]);
}
}
Code language: JavaScript (javascript)
Save as hello, make executable, and run:
chmod +x hello
./hello World
Output: Hello, World
- Note: Shebang files are platform-dependent and not supported natively on Windows.
Flexible Naming:
- The file name does not need to match the class name, unlike traditional Java compilation.
- Example: A file named Test.java can contain a class named MyProgram.
Command-Line Options:
- Use –source <version> to specify the Java version (e.g., –source 11).
- Supports options like –class-path for external libraries (e.g., JAR files) and –enable-preview for preview features.
- Example with a library:
java --class-path commons-lang3.jar MyProgram.java
Code language: CSS (css)
Use Cases
- Learning Java: Beginners can run simple programs without learning javac.
- Scripting: Write small utilities or scripts, especially with shebang support.
- Prototyping: Quickly test APIs or language features without setting up a project.
Limitations and Considerations
- Single File Only: All classes must be in one file. For multi-file programs, use javac or a build tool.
- No External .class Files: You can’t reference compiled classes outside the JDK.
- Not for Large Projects: Best suited for small, self-contained programs.
- Performance: Compilation happens in memory each time, so it’s not optimized for repeated execution.
Complementary Tools
- JShell: Pairing this feature with JShell (introduced in Java 9) creates a powerful toolset for learning and experimentation.
- Future Enhancements: JEP 458 (proposed later) extends this to multi-file programs, addressing some limitations.
This feature streamlines the process for simple Java programs, making it a valuable addition for both novices and experienced developers looking to reduce ceremony in small tasks.