Command-Line Java Execution: From JDK 8 to JDK 22
The article guides beginners through command‑line Java execution, showing how JDK 8 requires explicit compilation and class‑path, JDK 11 introduces single‑source file launching without compilation, and JDK 22 extends this to multi‑file programs, allowing direct execution of several source files with a simple java command.
For beginners who install IntelliJ IDEA and want to run a simple "Hello World" program, the usual workflow involves creating a Java source file, compiling it with javac , and then executing the resulting class with java . The article walks through this process step by step.
First, a minimal class Moon.java is shown:
public class Moon {
public static void main(String[] args) {
System.out.println("Hello");
}
}On JDK 8 you must compile the file:
javac Moon.javaand then run it with an explicit class‑path:
java -classpath . MoonIf the program references another class, e.g. Helper.java , both files need to be compiled together:
javac Moon.java Helper.javaand executed the same way:
java -classpath . MoonStarting with JDK 11 the launcher supports running a single source file directly, so the compilation step can be omitted:
java Moon.javaHowever, when multiple source files are involved (e.g., Moon.java calling Helper.sayHello() ), the old compile‑then‑run sequence is still required.
JDK 22 introduces an enhanced source‑file mode that allows a program consisting of several Java files to be launched directly:
java Moon.javaThis eliminates the need for an explicit compilation step for multi‑file projects, making the command‑line experience similar to interpreted languages like Python.
Java Tech Enthusiast
Sharing computer programming language knowledge, focusing on Java fundamentals, data structures, related tools, Spring Cloud, IntelliJ IDEA... Book giveaways, red‑packet rewards and other perks await!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.