JDK vs JRE vs JVM: The Three Layers

JDK vs JRE vs JVM: The Three Layers in Java: These three acronyms confuse almost every beginner, and they appear in nearly every Java interview.

Concept Explanation

These three acronyms confuse almost every beginner, and they appear in nearly every Java interview. The clean way to hold them is as nested layers:

+-------------------------------------------+
|  JDK  - Java Development Kit              |
|  javac, jar, javadoc, jdb  (build tools)  |
|  +-------------------------------------+  |
|  |  JRE - Java Runtime Environment     |  |
|  |  core libraries (String, List, ...) |  |
|  |  +-------------------------------+  |  |
|  |  |  JVM - Java Virtual Machine   |  |  |
|  |  |  executes bytecode            |  |  |
|  |  +-------------------------------+  |  |
|  +-------------------------------------+  |
+-------------------------------------------+
  • JVM — the engine. It reads .class bytecode and executes it. This is the piece that makes Java portable: a different JVM ships for each operating system, but bytecode stays identical.
  • JRE — the JVM plus the standard library. Enough to run Java, not to compile it.
  • JDK — the JRE plus developer tools, most importantly javac. This is what you install to write Java.

One line to remember: you develop on the JDK, users run on the JRE, and the JVM does the actual work.


Why "Write Once, Run Anywhere" Works

Hello.java  --javac-->  Hello.class  -->  Windows JVM
                         (bytecode)  -->  macOS JVM
                                     -->  Linux JVM

You compile once. The bytecode is platform-neutral, and each platform's JVM translates it into native instructions at runtime.


Real World Example

A bank builds a payments service on its developers' laptops using the JDK. The compiled artifact is deployed into a Linux container that only needs a JRE. In production, the JVM inside that container runs the bytecode and handles memory via garbage collection.


Common Mistakes

  1. "I have Java installed" but javac fails: You have a JRE. Install the JDK.
  2. Thinking Java is interpreted like Python: Java is compiled to bytecode first, then executed by the JVM. Two steps, not one.
  3. Believing bytecode is machine code: Bytecode is for the JVM. The JVM produces machine code, often just-in-time.

Best Practices

  • Ship the smallest runtime: Modern deployments use jlink to bundle only the modules the app needs.
  • Match versions: Bytecode compiled by JDK 21 will not run on a JVM 17. Compiling is forward-only.
  • Learn the tools: jar, javadoc, and jshell all ship with the JDK and are worth knowing.

Practice Exercises

  1. The Diagram: Redraw the three nested layers from memory and write one sentence describing each.
  2. The Bytecode Peek: Compile a class, then run javap -c Welcome to disassemble it. You are reading real bytecode.
  3. The Interview Answer: Write a three-sentence answer to "What is the difference between JDK, JRE, and JVM?" as if asked in an interview.