JDK, JRE, JVM and bytecode

JDK
Java Development Kit
JRE
Java Runtime Environment
JVM
Java Virtual Machine

Junior Always asked 7 min

On this page

The question

What is the difference between the JDK, the JRE and the JVM?

30-second answer

The JVM is the virtual machine that loads, verifies and runs bytecode and manages memory. The JRE is the JVM plus the standard class library: everything needed to run a program. The JDK is the JRE plus the development tools, such as javac, jar and javap: everything needed to build one. Since Java 11 Oracle ships only the JDK; jlink builds a trimmed runtime instead.

Understand it

From Hello.java to native code

Step by step

You write Hello.java: plain source text. No CPU and no JVM can run it yet.

Step 1 of 5
View as text

The program Hello has a method add() that main calls a million times. The drawing shows Hello.java and Hello.class at the top, and below them a JVM box holding a class loader, an interpreter, a JIT compiler and native code.

You write Hello.java: plain source text. No CPU and no JVM can run it yet. Only Hello.java is highlighted; the rest is faded.

javac compiles Hello.java into Hello.class. The class file holds bytecode, instructions for the JVM rather than for a real CPU. An arrow labelled javac joins the two files, and a note shows the bytecode of add(): iload_0, iload_1, iadd, ireturn.

java Hello starts a JVM. Its class loader finds Hello.class, loads it, and verifies the bytecode before any of it runs. An arrow leads from Hello.class into the class loader, marked verified.

The interpreter runs the bytecode one instruction at a time. It starts at once but runs slowly, and the JVM counts how often add() is called. A counter reads add() times 1,000,000.

add() runs a million times, so it is hot. The JIT compiler turns it into native code for this CPU, and later calls run that code. Arrows lead from the interpreter to the JIT compiler and on to native code, marked faster.

The program Hello has a method add() that main calls a million times. The drawing shows Hello.java and Hello.class at the top, and below them a JVM box holding a class loader, an interpreter, a JIT compiler and native code.

You write Hello.java: plain source text. No CPU and no JVM can run it yet. Only Hello.java is highlighted; the rest is faded.

javac compiles Hello.java into Hello.class. The class file holds bytecode, instructions for the JVM rather than for a real CPU. An arrow labelled javac joins the two files, and a note shows the bytecode of add(): iload_0, iload_1, iadd, ireturn.

java Hello starts a JVM. Its class loader finds Hello.class, loads it, and verifies the bytecode before any of it runs. An arrow leads from Hello.class into the class loader, marked verified.

The interpreter runs the bytecode one instruction at a time. It starts at once but runs slowly, and the JVM counts how often add() is called. A counter reads add() times 1,000,000.

add() runs a million times, so it is hot. The JIT compiler turns it into native code for this CPU, and later calls run that code. Arrows lead from the interpreter to the JIT compiler and on to native code, marked faster.

Takeaway: javac compiles source to portable bytecode; the JVM loads and verifies it, interprets it at first, and JIT-compiles the hot methods to native code.

Inside one running JVM

Tap the parts

Select a part to see what it does.

Tap a part of the JVM running Hello. The JVM specification defines the runtime areas; their names and layout here follow HotSpot.

View as text

Tap a part of the JVM running Hello. The JVM specification defines the runtime areas; their names and layout here follow HotSpot.

One JVM box holds six parts in two columns. Top row: the class loader, holding Hello.class, with an arrow into Metaspace, which holds class Hello. Middle row: thread stacks, with frames add and main on one stack and run on another, and an arrow from the main frame into the heap, which holds a String pointing at a byte array and one unreachable garbage object. Below them, across the whole width: the garbage collector, which frees garbage, then the execution engine, with an interpreter and a JIT.

Class loader: finds Hello.class the first time Hello is used, loads and verifies it, and asks its parent loaders first, up to the bootstrap loader that loads String.

Metaspace: HotSpot's home for the method area, holding each class's metadata and bytecode in native memory outside the heap since Java 8.

Thread stacks: one per thread, a frame per method call with its local variables.

Heap: every object and array, shared by all threads.

Execution engine: the interpreter runs bytecode at once and the JIT compiles hot methods to native code.

Garbage collector: frees heap objects that no GC root can reach.

Tap a part of the JVM running Hello. The JVM specification defines the runtime areas; their names and layout here follow HotSpot.

One JVM box holds six parts in two columns. Top row: the class loader, holding Hello.class, with an arrow into Metaspace, which holds class Hello. Middle row: thread stacks, with frames add and main on one stack and run on another, and an arrow from the main frame into the heap, which holds a String pointing at a byte array and one unreachable garbage object. Below them, across the whole width: the garbage collector, which frees garbage, then the execution engine, with an interpreter and a JIT.

Class loader: finds Hello.class the first time Hello is used, loads and verifies it, and asks its parent loaders first, up to the bootstrap loader that loads String.

Metaspace: HotSpot's home for the method area, holding each class's metadata and bytecode in native memory outside the heap since Java 8.

Thread stacks: one per thread, a frame per method call with its local variables.

Heap: every object and array, shared by all threads.

Execution engine: the interpreter runs bytecode at once and the JIT compiles hot methods to native code.

Garbage collector: frees heap objects that no GC root can reach.

Takeaway: One JVM loads classes into Metaspace, keeps objects on a shared heap and frames on per-thread stacks, runs bytecode with an interpreter and JIT, and collects garbage.

JDK, JRE and JVM nestedThe JDK box holds the development tools, such as javac, jar and javap, and the runtime. Inside it, the runtime box, the JRE, holds the JVM and the class library.JDKJRE: the runtimeTools: javac, jar,javapJVMClass library
JDK, JRE and JVM nestedThe JDK box holds the development tools, such as javac, jar and javap, and the runtime. Inside it, the runtime box, the JRE, holds the JVM and the class library.JDKJRE: the runtimeTools: javac, jar,javapJVMClass library
What the JDK, the JRE and the JVM each contain

In code

public class Hello {
    static int add(int a, int b) {
        return a + b;          // bytecode: iload_0 iload_1 iadd ireturn
    }

    public static void main(String[] args) {
        int sum = 0;
        for (int i = 0; i < 1_000_000; i++) {
            sum = add(sum, 1);  // called often: add() gets hot
        }
        System.out.println("Hello, " + sum);  // Hello, 1000000
    }
}
// javac Hello.java                  -> Hello.class
// java -XX:+PrintCompilation Hello  -> lists Hello::add as compiled
Hello: compiled once by javac, then interpreted and JIT-compiled by the JVM
ClassLoader app = Hello.class.getClassLoader();
System.out.println(app);                            // ...AppClassLoader@...
System.out.println(app.getParent());                // ...PlatformClassLoader@...
System.out.println(app.getParent().getParent());    // null: bootstrap
System.out.println(String.class.getClassLoader());  // null: bootstrap
Which class loader loaded each class
Class loader delegation for HelloThe application class loader asks the platform loader for Hello, which asks the bootstrap loader. Bootstrap does not find it and answers not found, the platform loader answers not found too, and the application loader then defines Hello from the class path.BootstrapPlatformApplicationload Helloload Hellonot foundnot founddefine Hello
Class loader delegation for HelloThe application class loader asks the platform loader for Hello, which asks the bootstrap loader. Bootstrap does not find it and answers not found, the platform loader answers not found too, and the application loader then defines Hello from the class path.BootstrapPlatformApplicationload Helloload Hellonot foundnot founddefine Hello
Loading Hello: each loader asks its parent first
javac Hello.java      # Hello.class holds bytecode, not machine code
javap -c Hello        # lists instructions such as getstatic, ldc and invokevirtual
java Hello            # the JVM loads, verifies, interprets, then JIT-compiles hot methods
java Hello.java       # Java 11+: compiles in memory and runs a single source file
Compile, read the bytecode, run

Solve it

Fix UnsupportedClassVersionError after a deploy

A service builds on laptops with JDK 21 and runs in a container image with a Java 17 runtime. It fails at startup: “has been compiled by a more recent version of the Java Runtime (class file version 65.0), this version of the Java Runtime only recognizes class file versions up to 61.0”. Explain and fix it.

  1. Decode the numbers. Class file major version 65 is Java 21 and 61 is Java 17. The bytecode is newer than the JVM running it.
  2. Know the direction. A newer JVM runs older bytecode, never the reverse. javac targets its own version unless told otherwise.
  3. Pick a fix. Prefer running on a Java 21 runtime, so build and run match. If the runtime must stay, compile with --release 17, which also rejects APIs added after 17.
  4. Prevent it. Pin the version in the build (maven.compiler.release or a Gradle toolchain) and in the image tag, and let CI (continuous integration) build with the same JDK.
javap -v Hello.class | grep major    # major version: 65  (built by JDK 21)
javac --release 17 Hello.java        # bytecode for 17; a Java 21-only API is now a compile error
javap -v Hello.class | grep major    # major version: 61  (runs on Java 17 and later)
Read the class version, then compile for the older runtime

How to answer

  • JVM: loads, verifies and runs bytecode, interpreting it or JIT-compiling it, and manages memory with garbage collection.
  • JRE = JVM + class library (java.base and the other runtime modules): what you need to run a program.
  • JDK = JRE + tools (javac, jar, javap, jlink, jshell): what you need to build one.
  • The bytecode is portable; each JVM is a native program built for one operating system and CPU.

Common mistakes

  • Saying the JVM compiles Java source code; javac compiles source to bytecode, and the JVM runs that bytecode.
  • Calling the JVM platform-independent, when it is the bytecode that is portable and each platform needs its own JVM build.

Follow-up questions

What is bytecode?

Bytecode is the instruction set of the JVM, stored in .class files. javac compiles each class to it. The instructions work on an operand stack rather than CPU registers, so return a + b in a static method becomes iload_0, iload_1, iadd, ireturn. Any JVM can run the same class file, interpreting it or compiling it to native code.

  • It is the output of javac and the input of the JVM, not machine code for any real CPU.
  • Instructions push and pop values on an operand stack.
  • javap -c prints a class's bytecode.
static int add(int a, int b) {
    return a + b;
}
// javap -c Hello:
//   0: iload_0
//   1: iload_1
//   2: iadd
//   3: ireturn

See it in the infographic: From Hello.java to native code

What does the JIT compiler do?

The just-in-time compiler turns frequently run bytecode into native machine code while the program runs. HotSpot interprets first and counts calls and loop iterations; hot methods are compiled quickly by C1, then again by C2 with heavier optimizations such as inlining, guided by the profile collected so far. If an assumption later breaks, the JVM deoptimizes back to the interpreter.

  • Only hot code is compiled, so startup stays fast and compile time goes where it pays off.
  • Tiered compilation, on by default since Java 8, uses C1 first and C2 for the hottest code.
  • Runtime profiles allow optimizations a compiler cannot prove ahead of time, such as inlining a virtual call.
  • -XX:+PrintCompilation lists each method as it is compiled.

See it in the infographic: From Hello.java to native code See it in the infographic: Inside one running JVM

How does class loading work in Java?

A class is loaded the first time it is used, not at startup. Loading reads the .class bytes; linking verifies them, prepares static fields with default values and resolves references; initialization runs the static initializers. Loaders ask their parent first, from application to platform to bootstrap, so core classes such as String always come from the bootstrap loader.

  • Three steps in order, loading, linking and initialization, each done once per class and loader.
  • Parent-first delegation stops an application from replacing a core class such as String.
  • String.class.getClassLoader() returns null: the bootstrap loader has no Java object.
  • The same class loaded by two loaders is two different runtime classes.
System.out.println(Hello.class.getClassLoader());
// jdk.internal.loader.ClassLoaders$AppClassLoader@...
System.out.println(String.class.getClassLoader());
// null: the bootstrap loader

See it in the infographic: Inside one running JVM

What does "write once, run anywhere" mean, and where does it stop being true?

javac produces bytecode for the JVM, not for a CPU, so the same .class or JAR (Java archive) file runs on any operating system with a compatible JVM. It stops at native code reached through JNI (Java Native Interface), at OS-specific details such as file paths, and at older runtimes: a class compiled for Java 21 fails on Java 17 with UnsupportedClassVersionError.

  • Portability comes from the bytecode and the JVM specification, not from the JVM itself.
  • Each class file records a version; a JVM refuses class files newer than it supports.
  • javac --release N builds class files and API checks for an older Java release.

See it in the infographic: From Hello.java to native code

What is the trade-off between JIT compilation and ahead-of-time compilation?

A JIT compiles while the program runs, using a profile of how the code actually behaves, so peak throughput is high, but startup is slower, warm-up takes time and the compiler uses memory. Ahead-of-time compilation, as in GraalVM Native Image, compiles before the program runs: it starts in milliseconds with a small footprint, but assumes a closed world, so reflection and dynamic class loading need configuration.

  • JIT: slow start, best peak throughput for long-running services.
  • AOT: instant start and low memory, a good fit for serverless functions and CLI (command-line interface) tools.
  • AOT needs to see all reachable code at build time, so reflection must be declared.

See it in the infographic: From Hello.java to native code

Is there still a JRE?

Not from Oracle: since Java 11 it ships only the JDK. You build a runtime with jlink that holds just the modules your application needs, and some vendors, such as Eclipse Temurin, still publish JRE builds.

Is the JVM platform-independent?

No. The bytecode is platform-independent; the JVM is a native program built for each operating system and CPU. Every JVM implements the same specification, so the same class file runs on all of them.

What is JIT warm-up?

A fresh JVM interprets the bytecode first, so early requests are slow. As methods get hot, the JIT compiles and optimizes them, and throughput climbs over the first seconds or minutes. Benchmarks and load tests discard this warm-up phase before they measure.

Check yourself

1. Which tool turns Hello.java into Hello.class?
Show answers
  • javac, the compiler in the JDK: Correct. javac compiles source to bytecode before any JVM starts.
  • The JVM's class loader: Incorrect. The class loader reads an existing class file; it does not compile source.
  • The JIT compiler: Incorrect. The JIT compiles bytecode to native code at runtime, not source to bytecode.
2. A method of a running program is called a million times. What does HotSpot typically do with it?
Show answers
  • The JIT compiles it to native code, and later calls run that code: Correct. Hot methods are compiled at runtime, which is why the program speeds up after warm-up.
  • javac recompiles the source file to faster bytecode: Incorrect. javac runs once, before the program starts; the JVM never calls it.
  • It keeps interpreting it, one bytecode at a time: Incorrect. The interpreter only runs code until it gets hot; hot code is JIT-compiled.
3. What does this print?
System.out.println(String.class.getClassLoader());
Show answers
  • null: Correct. String is loaded by the bootstrap loader, which has no Java object.
  • The application class loader: Incorrect. The application loader asks its parents first, so core classes come from bootstrap.
  • The platform class loader: Incorrect. The platform loader also delegates up; String lives in java.base, loaded by bootstrap.