How Java compilation and the JVM work
JDK vs JRE vs JVM, compilation steps, bytecode, class files, platform independence, javac command
Java Compilation Pipeline
Java source code does not run directly on your machine. It goes through two stages before execution.
Stage 1 — Compile: javac reads your .java file and produces a .class file containing platform-neutral bytecode. No machine-specific instructions exist yet. The bytecode is an intermediate representation designed for the JVM, not for any real CPU.
Stage 2 — Run: The Java Virtual Machine (JVM) reads that bytecode and translates it to native machine instructions at runtime using a JIT (Just-In-Time) compiler. This is why the same .class file runs on Windows, macOS, and Linux without recompilation — the JVM on each platform handles translation.
The JDK (Java Development Kit) bundles javac, the JVM, and standard libraries — everything a developer needs. The JRE (Java Runtime Environment) contains only the JVM and libraries, enough to run but not compile Java programs. Modern JDK distributions include both.
Your First Compile-and-Run
// File: Hello.java
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, Java");
}
}
# Terminal
javac Hello.java # produces Hello.class
java Hello # JVM executes bytecode
# Output: Hello, Java
The class name must match the filename exactly, including case. main is the entry point the JVM looks for — its signature public static void main(String[] args) must be exact. Changing any modifier causes a runtime error, not a compile error.
