JDK, JRE, and JVM – Complete Guide with Architecture, Examples, and Differences


Learn the difference between JDK, JRE, and JVM, their roles in Java development, how Java code executes, and understand the Java execution hierarchy with examples and diagrams.

JDK, JRE, and JVM – Complete Detailed Tutorial

Understanding JDK, JRE, and JVM is essential for every Java developer. These three components define how Java code is written, compiled, and executed.

1. What is JVM (Java Virtual Machine)?

JVM is the engine that executes Java bytecode. It is platform-dependent but runs bytecode on any OS through its runtime environment.

Key Roles of JVM

  1. Loads class files (.class)
  2. Verifies the bytecode
  3. Executes bytecode

1.1 JVM Architecture

JVM consists of several components:

  1. Class Loader Subsystem
  2. Loads .class files at runtime.
  3. Supports dynamic class loading.
  4. Runtime Data Areas
  5. Method Area: Stores class structures, constants, method info
  6. Heap: Stores objects
  7. Stack: Stores frames for method calls
  8. PC Register: Points to current instruction
  9. Native Method Stack: Stores native method information
  10. Execution Engine
  11. Interpreter: Reads bytecode line by line
  12. JIT Compiler: Converts bytecode to native machine code for faster execution
  13. Garbage Collector: Frees unused memory automatically

1.2 JVM Bytecode Execution Flow


Java Source Code (.java)
↓ compile (javac)
Java Bytecode (.class)
↓ run (java)
JVM executes bytecode on any OS

Key Points:

  1. JVM is platform-dependent.
  2. Bytecode is platform-independent.
  3. Enables WORA (Write Once, Run Anywhere).

2. What is JRE (Java Runtime Environment)?

JRE provides the environment to run Java applications.

Components of JRE

  1. JVM → Executes bytecode
  2. Core Libraries → Predefined Java classes (java.lang, java.util)
  3. Runtime Environment → Supports Java program execution

Key Point

  1. JRE cannot compile Java programs.
  2. Required only for running Java applications, not developing.

3. What is JDK (Java Development Kit)?

JDK is a full software development kit for Java.

It contains:

  1. JRE → To run Java programs
  2. JVM → To execute bytecode
  3. Development Tools:
  4. javac → Compiler
  5. java → Launcher
  6. javadoc → Documentation
  7. jar → Packaging tool
  8. jdb → Debugger

Use Case

  1. Developers need JDK to write, compile, and run Java programs.
  2. Users only need JRE to run Java programs.

4. Relationship Between JDK, JRE, and JVM


JDK
└── JRE
└── JVM
  1. JDK → Tools for developers (compile + run)
  2. JRE → Runtime environment (run programs only)
  3. JVM → Executes bytecode

Example Analogy: Making Tea

ComponentAnalogy
JVMStove that executes the tea-making process
JREStove + ingredients + utensils (ready environment to make tea)
JDKEverything in JRE + tools to prepare tea (for development)

5. Key Differences – JDK vs JRE vs JVM

FeatureJDKJREJVM
Full FormJava Development KitJava Runtime EnvironmentJava Virtual Machine
PurposeDevelopment + ExecutionExecution onlyExecutes bytecode
ContainsJRE + ToolsJVM + LibrariesBytecode interpreter & execution engine
Toolsjavac, jar, javadocNoneNone
Who UsesDevelopersEnd usersBoth

6. Example of Compilation & Execution

Step 1: Write Java Code


class Hello {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}

Step 2: Compile with JDK


javac Hello.java
  1. Creates Hello.class (bytecode)

Step 3: Run with JRE/JVM


java Hello
  1. JVM executes bytecode and prints output.

7. Summary

  1. JVM: Executes bytecode (platform-dependent)
  2. JRE: Provides environment to run Java programs
  3. JDK: Contains JRE + tools to develop Java programs

Key Concept:

  1. JDK → Development + Runtime
  2. JRE → Runtime
  3. JVM → Execution