Sub topic 1
From Source File to Running Program: The JVM Pipeline
Prerequisites
This chapter assumes that you can write and run basic Java programs. You should know classes, methods, static members, and exceptions. You should also be comfortable using javac and java from a terminal.
The path from source to execution
A Java program passes through several stages:
- You write source code in a
.javafile. javaccompiles the source into a.classfile.- The class file stores platform-independent bytecode and metadata.
- The
javalauncher starts a Java Virtual Machine (JVM). - The JVM loads the required classes.
- It links and initializes those classes.
- An execution engine runs the bytecode.
The JVM does not normally execute Java source code directly. The compiler translates source into bytecode, and the JVM executes that bytecode on the target operating system.
Compile source into a class file
Consider this complete program:
public class Greeting {
public static void main(String[] args) {
System.out.println("Hello from the JVM");
}
}Save it as Greeting.java. The public class name and file name must match. Compile and run it with:
javac --release 11 Greeting.java
java GreetingThe compiler creates Greeting.class. The --release 11 option asks javac to produce Java 11-compatible class-file output. The java command looks for the Greeting class, loads it, and invokes its main method.
Compilation can fail before the JVM starts. For example, a missing semicolon produces a compiler error. A successful compilation does not guarantee successful execution. The program can still fail when the JVM cannot find a required class or when a method throws an exception.
What the JVM does at startup
The JVM begins by locating the initial class. It uses class loaders to obtain the class definition. Loading is followed by linking, which prepares the class for use. Linking includes verification of bytecode, preparation of static storage, and optional resolution of symbolic references.
The JVM initializes a class when execution first requires it. Initialization runs static field initializers and static initializer blocks in a defined order. For the example, initialization prepares Greeting before its main method runs.
The execution engine can interpret bytecode instruction by instruction. Code that runs frequently may be compiled by the just-in-time (JIT) compiler into native machine code. This lets the JVM keep portability while optimizing hot parts of a running program.
Common boundaries and errors
Keep these boundaries separate:
javacerrors happen during compilation.ClassNotFoundExceptionusually means code explicitly tried to load a class that was not found.NoClassDefFoundErroroften means a class was available during compilation but missing or unusable at runtime.- An exception from
mainmeans the JVM started the program, but application code failed during execution.
The JVM pipeline is therefore a sequence of contracts. The compiler must produce valid class files, the runtime must find and prepare them, and the execution engine must run their bytecode.
Exercise
Which command creates Greeting.class, and which command starts the JVM and runs main?
Show answer
javac --release 11 Greeting.java creates the class file. java Greeting starts the JVM and runs main.