- 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
This generates:
HelloWorld.class
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
Output:
Hello, Java!
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)