What happens when you run a Java program?
Fast recall
Quick revision
Core answer
java HelloWorld runs a traditional class, the launcher starts a JVM, locates the initial class through the configured class path or module path, loads and links it, initializes it, invokes its accepted main entry point, loads dependencies as needed, and executes bytecode using that JVM's runtime strategy.Interview-ready guide
Interview answer
Running java HelloWorld starts the Java launcher and asks it to use HelloWorld as the initial class. Options before the class name can set the class path, module path, system properties, or memory settings.
The JVM locates and loads the class. Linking then verifies the class file, prepares static fields with default values, and resolves symbolic references when required. Initialization runs static field initializers and static blocks before normal use of the class.
The launcher then invokes the conventional public static void main(String[] args) entry point. For example, java -cp out HelloWorld Ravi searches out for the class and passes Ravi as args[0]. The -cp out part configures the launcher and does not enter the application's argument array.
As main runs, more classes are loaded when needed. Method calls create frames, objects and arrays use heap storage, garbage collection manages unreachable objects, and HotSpot may JIT-compile hot bytecode. The process normally stays alive while non-daemon threads remain and ends when the JVM's termination rules are met or an explicit exit occurs.
What happens when you run a Java program
Direct Answer
When java HelloWorld runs a traditional class, the launcher starts a JVM, locates the initial class through the configured class path or module path, loads and links it, initializes it, invokes its accepted main entry point, loads dependencies as needed, and executes bytecode using that JVM's runtime strategy.
Deep Dive & Architecture
Java execution phases and their failures
“Could not find or load main class” means the launcher could not locate or load the requested initial class with the supplied class/module path. An unsupported class-file version means the runtime is older than the class-file format it was asked to accept. VerifyError points to verification, while a linkage error can reveal an incompatible class or missing member. NoClassDefFoundError during execution can mean a needed definition is unavailable or that a previous initialization attempt failed.
Initialization happens before visible main work
Before ordinary use of the initial class, the JVM initializes it as required. Static field initializers and static blocks run in class-initialization order. A failed static initializer prevents normal entry, and later uses can observe the recorded initialization failure. A long or circular initializer can make startup appear hung even though the launcher found the class correctly.
Arguments have two owners
In java -cp out Hello Ravi, -cp out configures the launcher, Hello names the initial class, and Ravi becomes an application argument. Mixing their order can change whether the launcher or main receives a value. Dependencies may then be loaded and initialized on demand as code reaches them.
Termination is also a lifecycle
The process normally remains alive while non-daemon threads are active. Shutdown hooks run during orderly shutdown, but abrupt process termination, an operating-system failure, or forced halt can skip them. Durable work needs a database, file, queue, or other external contract rather than an assumption that a final in-memory callback always runs.
Code Example
class HelloWorld {
static {
System.out.println("class initialized");
}
public static void main(String[] args) {
String name = args.length == 0 ? "world" : args[0];
System.out.println("Hello, " + name);
}
}
Compile with javac -d out HelloWorld.java, then run java -cp out HelloWorld Ravi.
Check your understanding
Answer these without looking back at the article.
- What is the difference between loading, linking, and initialization?
- Which command-line arguments become the main method's args array?
- Why can a class-initialization error occur before main executes?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.