Introduction
If you’re new to Java, you’ve probably come across the terms JVM, JRE, and JDK. Although these three terms are closely related, they serve different purposes in the Java ecosystem. Many beginners often confuse them because they work together to compile, execute, and develop Java applications.
Understanding the difference between Java Virtual Machine (JVM), Java Runtime Environment (JRE), and Java Development Kit (JDK) is one of the most important topics in Java programming. It is also a frequently asked question in Java interviews.
In simple words:
- JVM executes Java bytecode.
- JRE provides the environment to run Java programs.
- JDK provides tools to develop, compile, debug, and run Java applications.
In this tutorial, you’ll learn what JVM, JRE, and JDK are, how they work together, their differences, real-life examples, common mistakes, and interview questions.
What is JVM?
JVM (Java Virtual Machine) is a virtual machine responsible for executing Java bytecode.
Java source code cannot run directly on your operating system. Instead, it is first compiled into bytecode (.class files), and the JVM converts this bytecode into machine code that your operating system understands.
Responsibilities of JVM
- Loads Java classes.
- Verifies bytecode.
- Executes Java programs.
- Manages memory.
- Performs Garbage Collection.
- Handles exceptions.
- Supports multithreading.
JVM Architecture
Major components include:
- Class Loader
- Runtime Data Areas
- Execution Engine
- Garbage Collector
- Java Native Interface (JNI)
- Native Libraries
Example
When you execute:
java HelloWorld
The JVM:
- Loads HelloWorld.class.
- Verifies the bytecode.
- Converts bytecode into machine code.
- Executes the program.
What is JRE?
JRE (Java Runtime Environment) is the software package required to run Java applications.
It provides everything needed to execute Java programs but does not include development tools such as the Java compiler.
JRE Contains
- JVM
- Core Java Libraries
- Supporting configuration files
- Runtime resources
Purpose of JRE
- Runs Java applications.
- Provides Java APIs.
- Supplies the runtime environment.
Every Java developer installs the JDK.
Relationship Between JVM, JRE, and JDK
The relationship can be understood as:
JDK
│
├── JRE
│ │
│ └── JVM
Or:
JDK = JRE + Development Tools
JRE = JVM + Java Libraries
JVM = Executes Bytecode
This shows that:
- Every JDK contains a JRE.
- Every JRE contains a JVM.
- JVM is the core execution engine.
Java Program Execution Process
The following diagram shows how JVM, JRE, and JDK work together.
Java Source Code (.java)
│
▼
JDK (javac)
│
▼
Bytecode (.class)
│
▼
JRE
│
▼
JVM
│
▼
Machine Code
│ ▼
Program Output
Step-by-Step Process
- Write Java source code.
- Compile using the JDK (javac).
- Generate bytecode (.class).
- The JRE provides the runtime environment.
- The JVM loads and executes the bytecode.
- The operating system executes the generated machine code.
JVM vs JRE vs JDK
| Feature | JVM | JRE | JDK |
|---|---|---|---|
| Full Form | Java Virtual Machine | Java Runtime Environment | Java Development Kit |
| Purpose | Executes bytecode | Runs Java applications | Develops Java applications |
| Includes JVM | ✔ | ✔ | ✔ |
| Includes JRE | ✖ | ✔ | ✔ |
| Compiler (javac) | ✖ | ✖ | ✔ |
| Debugging Tools | ✖ | ✖ | ✔ |
| Documentation Tool | ✖ | ✖ | ✔ |
| Java Libraries | Partial | ✔ | ✔ |
| Used By | JVM internally | End Users | Java Developers |
| Can Compile Code | ✖ | ✖ | ✔ |
| Can Run Programs | ✔ | ✔ | ✔ |
Real-Life Example
Imagine you’re building a house.
JDK
The JDK is like a complete toolbox.
It contains:
- Hammer
- Saw
- Drill
- Measuring tape
- Screwdriver
Everything required to build the house.
JRE
The JRE is like a finished house.
People can live inside it, but they cannot build another house using it.
JVM
The JVM is like the electricity system inside the house.
It makes everything work but cannot build the house by itself.
This analogy helps understand the relationship:
- Builder → JDK
- House → JRE
- Electricity → JVM
Example
Consider the following Java program:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello Java");
}
}
Compilation
javac HelloWorld.java
The JDK compiles the program into:
HelloWorld.class
Execution
java HelloWorld
The JRE starts the runtime environment.
The JVM loads the bytecode and executes it.
Output:
Why Do We Need All Three?
Each component has a different responsibility.
JVM
- Executes bytecode.
- Manages memory.
- Performs garbage collection.
JRE
- Provides runtime libraries.
- Includes the JVM.
- Executes Java applications.
JDK
- Compiles source code.
- Generates bytecode.
- Includes development tools.
- Contains the JRE.
Without the JDK, developers cannot compile Java programs.
Without the JVM, Java programs cannot execute.
Without the JRE, the runtime environment and required libraries are unavailable.
Advantages
JVM
- Platform independence
- Automatic memory management
- Security
- High performance with JIT compilation
JRE
- Easy execution of Java applications
- Provides standard libraries
- Cross-platform support
JDK
- Complete development toolkit
- Compiler included
- Debugging support
- Documentation generation
- Packaging tools
Common Mistakes
Beginners often misunderstand JVM, JRE, and JDK.
1. Thinking JVM and JDK Are the Same
Incorrect.
The JVM only executes bytecode, while the JDK contains the tools required to develop Java applications.
2. Assuming JRE Can Compile Java Programs
Incorrect.
Compilation requires the javac compiler, which is part of the JDK.
3. Believing JVM Runs Source Code Directly
Incorrect.
The JVM executes bytecode, not .java source files.
4. Installing Only a Runtime When Learning Java
If you’re learning Java programming, install a JDK rather than only a runtime environment so you have access to the compiler and development tools.
5. Confusing Bytecode with Machine Code
Bytecode is platform-independent intermediate code.
The JVM converts it into machine code during execution.
Best Practices
- Install the latest Long-Term Support (LTS) JDK.
-
Verify the installation using:
java -version
javac -version - Keep the JDK updated.
- Understand the Java execution process.
- Learn the role of the JVM before exploring advanced topics such as garbage collection and memory management.
- Use an IDE like IntelliJ IDEA or Eclipse to simplify development.
Conclusion
Understanding the differences between JVM, JRE, and JDK is essential for every Java developer. While they work together, each serves a distinct purpose. The JVM is responsible for executing Java bytecode and managing memory. The JRE provides the runtime environment and libraries needed to run Java applications. The JDK is the complete development toolkit that includes the JRE, the JVM, and tools such as the Java compiler, debugger, and documentation generator.
In simple terms:
- JVM = Executes Java bytecode.
- JRE = Provides the environment to run Java applications.
- JDK = Provides everything needed to develop, compile, debug, and run Java applications.