DEV Community

Kavitha
Kavitha

Posted on

How to Run a Java Program Using the Command Line

  • Running a Java program from the command line helps you understand how the JVM executes bytecode and is a fundamental skill for Java developers, especially for interviews and backend development.

Step 1: Ensure the Program Is Compiled
Before running a Java program, it must be compiled.

javac HelloWorld.java

Enter fullscreen mode Exit fullscreen mode

This generates:

HelloWorld.class

Enter fullscreen mode Exit fullscreen mode

Step 2: Navigate to the Program Directory
Open Command Prompt / Terminal and move to the directory containing the .class file

Step 3: Run the Java Program
Use the java command followed by the class name (without .class):

java HelloWorld

Enter fullscreen mode Exit fullscreen mode

Output:

Hello, Java!

Enter fullscreen mode Exit fullscreen mode

What Happens Internally When You Run a Java Program?

  • JVM starts execution
  • Class Loader loads the .class file
  • Bytecode is verified
  • JVM executes the main() method
  • JIT compiles frequently used code for better performance

Top comments (0)