Launch Single-File Source-Code Programs
Before Java 11, the process of going from source code to execution was a two-step process: you compiled the code and then ran the code. Beginning with Java 11, another way to run a Java program was introduced, called Launch Single-File Source-Code Programs. This allows you to compile, start up the JVM, and execute the program in a single line. We’ll see how this works for Windows, macOS, and Linux before examining a unique way for Linux and macOS.
For Windows, macOS, and Linux
Open a Command Prompt or Terminal in the same folder as the file you wish to run and, if necessary, update the Path
and JAVA_HOME
properties. Now, simply enter java
and the name of the source file:
Figure 2.9 – Running a Java program
As the name of this technique implies, your program can only consist of a single file. This source file may contain more than one class and the first class in the file must have a main
method...