In this tutorial, we learn more about how to write single file source code with Java 11, looking at example code and CLIs.
Let’s be friends:
Comment (0)
Join the DZone community and get the full member experience.
One of the many salient features of Java 11 is the ability to directly run a single file of Java source code without the need to compile the source code first. Complementing JShell, this feature helps in learning Java and in writing simple utility applications and CLIs.
Say you created a HelloWorld.java file that, you guessed it, prints “Hello World:”
With JDK 11 installed, you can run this file using the “java” launcher:
The “java” launcher in JDK 11 has been updated to operate in four modes:
The “java” launcher identifies the single file mode when one of the following two conditions match:
As mentioned earlier, the single file mode can be useful to create small utility programs.