Fundamentals 5 min read

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.

Java Tech Enthusiast
Java Tech Enthusiast
Java Tech Enthusiast
Command-Line Java Execution: From JDK 8 to JDK 22

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.java

and then run it with an explicit class‑path:

java -classpath . Moon

If the program references another class, e.g. Helper.java , both files need to be compiled together:

javac Moon.java Helper.java

and executed the same way:

java -classpath . Moon

Starting with JDK 11 the launcher supports running a single source file directly, so the compilation step can be omitted:

java Moon.java

However, 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.java

This eliminates the need for an explicit compilation step for multi‑file projects, making the command‑line experience similar to interpreted languages like Python.

JavaCompilationJDKcommand lineTutorial
Java Tech Enthusiast
Written by

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!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.