JDK vs JRE vs JVM
Reading Time: 7 Minutes
Difficulty: Beginner
Topic Summaryโ
JDK, JRE, and JVM are three key components of the Java ecosystem. Every Java program needs all three to work. Understanding the difference helps you know what to install and how Java programs actually run on your computer.
What You'll Learnโ
- What JVM is and what it does
- What JRE is and what it contains
- What JDK is and what extras it provides
- How JDK, JRE, and JVM relate to each other
- When you need each one
Prerequisitesโ
- Lesson 001 โ What is Java
- Lesson 003 โ Features of Java (platform independence concept)
Explanationโ
JVM โ Java Virtual Machineโ
The JVM (Java Virtual Machine) is the most fundamental piece. It is a virtual computer inside your real computer. Its job is to:
- Load the compiled Java bytecode (
.classfiles) - Verify the bytecode is safe to run
- Execute the bytecode โ running your Java program
- Manage memory โ garbage collection happens here
The JVM is what makes Java platform independent. Each operating system has its OWN version of JVM (Windows JVM, Mac JVM, Linux JVM), but they all understand the same bytecode. So you write once, the JVM runs it everywhere.
Key point: The JVM does NOT understand Java source code (.java files). It only understands bytecode (.class files).
What's inside JVM?โ
- Class Loader โ loads your
.classfiles into memory - Bytecode Verifier โ checks the code is valid and safe
- Interpreter โ executes bytecode instructions
- JIT Compiler โ compiles hot bytecode to native machine code for speed
- Garbage Collector โ frees unused memory automatically
JRE โ Java Runtime Environmentโ
The JRE (Java Runtime Environment) is everything you need to RUN a Java program. It contains:
- โ The JVM (to execute bytecode)
- โ Java standard libraries (pre-written classes like String, Math, ArrayList)
- โ Supporting files and configuration
If you just want to RUN Java programs (not write them), you install the JRE. For example, a user who just wants to play a Java game โ they need JRE, not JDK.
Note: Since Java 11, Oracle no longer ships a standalone JRE download. The JDK includes the JRE functionality. For Java 11+, just install the JDK.
JDK โ Java Development Kitโ
The JDK (Java Development Kit) is everything you need to WRITE, COMPILE, AND RUN Java programs. It contains:
- โ Everything in JRE (which includes JVM)
- โ
javac โ the Java compiler (turns
.javaโ.class) - โ javadoc โ generates documentation from comments
- โ jar โ creates JAR archive files
- โ jdb โ Java debugger
- โ jshell โ interactive Java REPL (since Java 9)
- โ Other development tools
If you want to WRITE Java programs, you need the JDK. Always install JDK as a developer.
The Relationship โ Nested Like Boxesโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ JDK โ
โ (Java Development Kit) โ
โ โ
โ javac, javadoc, jar, jdb, jshell... โ
โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ JRE โ โ
โ โ (Java Runtime Environment) โ โ
โ โ โ โ
โ โ Java Standard Libraries โ โ
โ โ (java.lang, java.util, etc.) โ โ
โ โ โ โ
โ โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ โ
โ โ โ JVM โ โ โ
โ โ โ (Java Virtual Machine) โ โ โ
โ โ โ โ โ โ
โ โ โ Class Loader โ โ โ
โ โ โ Bytecode Verifier โ โ โ
โ โ โ Interpreter + JIT โ โ โ
โ โ โ Garbage Collector โ โ โ
โ โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Think of it as nested Russian dolls โ JVM is inside JRE, JRE is inside JDK.
Quick Comparison Tableโ
| JVM | JRE | JDK | |
|---|---|---|---|
| Full Name | Java Virtual Machine | Java Runtime Environment | Java Development Kit |
| Purpose | Execute bytecode | Run Java programs | Develop Java programs |
| Contains | Interpreter, JIT, GC | JVM + Libraries | JRE + javac + tools |
| Who needs it? | (part of JRE) | End users | Developers |
| Can compile Java? | โ No | โ No | โ Yes |
| Can run Java? | โ Yes | โ Yes | โ Yes |
How They Work Togetherโ
When you write and run a Java program:
Step 1: You write HelloWorld.java
Step 2: JDK's javac compiles it โ HelloWorld.class (bytecode)
Step 3: JRE's class loader loads HelloWorld.class
Step 4: JVM executes the bytecode โ Output appears!
Real-World Analogyโ
Imagine you're in the film industry:
-
๐ฌ JVM = The movie projector. It knows how to play films (bytecode). But it needs the right film reel format. It can't CREATE films, only show them.
-
๐๏ธ JRE = The cinema (projector + screen + seats + sound system). Everything needed to watch a movie. Audiences (end users) need this.
-
๐ฅ JDK = The full film studio (cinema + cameras + editing tools + scriptwriting software). Directors (developers) need this to CREATE films and show them too.
A moviegoer just needs the cinema (JRE). A filmmaker needs the full studio (JDK).
Code Exampleโ
// This program demonstrates what the JDK, JRE, and JVM do
public class JdkJreJvm {
public static void main(String[] args) {
// The JDK compiled this file using javac
// The JRE loaded Java standard libraries like System, String
// The JVM is executing THIS line right now!
System.out.println("=== Understanding JDK, JRE, JVM ===\n");
System.out.println("JVM โ Executes bytecode (this program runs ON the JVM)");
System.out.println("JRE โ JVM + Standard Libraries (String, System, Math...)");
System.out.println("JDK โ JRE + javac + javadoc + jar + dev tools");
// Using JRE's standard library (Math class)
double result = Math.sqrt(144);
System.out.println("\nMath.sqrt(144) = " + result);
// Math is part of JRE's standard library!
// JVM information at runtime
System.out.println("\n--- Your JVM Info ---");
System.out.println("Java Version: " + System.getProperty("java.version"));
System.out.println("JVM Name: " + System.getProperty("java.vm.name"));
System.out.println("OS: " + System.getProperty("os.name"));
}
}
Outputโ
=== Understanding JDK, JRE, JVM ===
JVM โ Executes bytecode (this program runs ON the JVM)
JRE โ JVM + Standard Libraries (String, System, Math...)
JDK โ JRE + javac + javadoc + jar + dev tools
Math.sqrt(144) = 12.0
--- Your JVM Info ---
Java Version: 21.0.1
JVM Name: OpenJDK 64-Bit Server VM
OS: Windows 11
Common Mistakesโ
- โ Mistake: Installing JRE to write Java programs โ โ
Fix: Developers always need the JDK. JRE alone doesn't include
javac(the compiler). - โ Mistake: Thinking JVM is the same on all platforms โ โ Fix: Each OS has a different JVM implementation, but they all execute the same bytecode (that's the magic!).
- โ Mistake: Confusing bytecode with machine code โ โ Fix: Bytecode is an intermediate format only the JVM understands. Machine code is what your CPU directly executes.
Best Practicesโ
- As a developer, always install the JDK โ it includes everything
- Use the latest LTS version of the JDK (currently JDK 21)
- When deploying an app to a server, you might only need the JRE (or JDK โ both work)
- Use
java -versionto check your Java version andjavac -versionto verify the compiler
Interview Questionsโ
Q: What is the difference between JDK, JRE, and JVM?
A: JVM (Java Virtual Machine) executes bytecode. JRE (Java Runtime Environment) = JVM + standard libraries โ needed to run Java programs. JDK (Java Development Kit) = JRE + compiler (javac) + dev tools โ needed to develop Java programs.
Q: Is JVM platform dependent or independent?
A: The JVM itself is platform dependent (there's a Windows JVM, Mac JVM, etc.). But the bytecode that runs ON the JVM is platform independent. That's why "Write Once, Run Anywhere" works.
Q: Can you run Java without JDK?
A: Yes โ if you only want to run (not develop) Java programs, you need only the JRE (which contains the JVM). However, to compile and develop, you need the full JDK.
Q: What is the JIT compiler?
A: JIT (Just-In-Time) compiler is part of the JVM. It monitors which bytecode is executed frequently (hot code) and compiles that to native machine code for faster execution, giving Java near-native performance.
Quick Revisionโ
โ JVM = Java Virtual Machine โ executes bytecode, inside JRE
โ JRE = JVM + standard libraries โ runs Java programs
โ JDK = JRE + javac + dev tools โ develops Java programs
โ JVM is platform dependent, bytecode is platform independent
โ Developers install JDK; end users (historically) needed JRE
โ JDK 11+ bundles everything โ just install the JDK
Related Topicsโ
- Java Program Execution Flow (Lesson 005)
- Installing Java (Lesson 006)
- Features of Java โ Platform Independence (Lesson 003)
Next Lessonโ
Lesson 005 โ Java Program Execution Flow