How do the JDK, JRE and JVM differ, and how does that split make Java platform independent?
The JVM (Java Virtual Machine) runs bytecode: it loads .class files, interprets them and compiles hot code to machine code with its JIT compiler. The JRE is the JVM plus the standard class libraries: everything needed to run Java programs. The JDK is the JRE plus development tools such as javac, jar and javadoc: everything needed to build them. Java is platform independent because javac produces the same bytecode everywhere, and each operating system has its own JVM to run it.
Full answerHide full answer2 min read
JDK = JRE + javac, jar, javadoc, jdb, jshell… (develop)
JRE = JVM + standard libraries (java.lang, java.util…) (run)
JVM = class loader + bytecode verifier + interpreter + JIT compiler + garbage collector
Write once, run anywhere
Hello.java ──javac──▶ Hello.class (bytecode, the same on every OS)
│
┌───────────────────┼───────────────────┐
▼ ▼ ▼
JVM for Windows JVM for Linux JVM for macOS ← each is platform-specific
The bytecode is portable; the JVM is not. That's the whole trick: Java moves the platform-specific part into the JVM, so your compiled program doesn't change.
javac Hello.java # compile: produces Hello.class
java Hello # run: the JVM loads and executes the bytecode
java Hello.java # Java 11+: compile in memory and run a single-file program| JDK | JRE | JVM | |
|---|---|---|---|
| Contains | JRE + tools | JVM + libraries | The runtime engine |
| Needed to | Write and compile | Run | Execute bytecode |
Includes javac | Yes | No | No |
A change worth mentioning: since Java 11, Oracle no longer ships a separate JRE download. You install a JDK, or build a slimmed-down runtime for your app with jlink.
Follow-ups
- Is Java compiled or interpreted? Both: compiled to bytecode ahead of time, then interpreted and JIT-compiled to machine code at runtime.
- What does the JIT compiler do? It watches which methods run often ("hot" code) and compiles them to optimised native code, which is why long-running Java programs speed up after warming up.