Lesson 1.4: JDK, JRE, and JVM
Introduction
One of the most common interview questions in Java is:
“What is the difference between JDK, JRE, and JVM?”
Many beginners get confused because these three terms are closely related. However, each has a different purpose.
In this lesson, you will learn what JDK, JRE, and JVM are, how they work together, and how a Java program is executed.
Learning Objectives
After completing this lesson, you will be able to:
-
Define JDK, JRE, and JVM.
-
Understand the role of each component.
-
Explain the Java program execution process.
-
Differentiate between JDK, JRE, and JVM.
-
Understand why Java is platform independent.
What Happens When You Run a Java Program?
When you write a Java program, it does not run directly on your computer.
Instead, Java follows these steps:
-
Write the Java source code.
-
Compile the source code into bytecode.
-
Load the bytecode into the JVM.
-
JVM converts the bytecode into machine code.
-
The operating system executes the machine code.
This process allows the same Java program to run on different operating systems.
What is JDK?
JDK stands for Java Development Kit.
It is a complete package used to develop Java applications.
The JDK contains everything required to write, compile, debug, and run Java programs.
Main Components of JDK
-
JRE
-
Java Compiler (
javac) -
Debugging Tools
-
Documentation Tools
-
Java Libraries
-
Development Utilities
Purpose of JDK
-
Write Java programs
-
Compile Java programs
-
Run Java programs
-
Debug Java applications
Developers install the JDK.
What is JRE?
JRE stands for Java Runtime Environment.
It provides everything needed to run Java applications, but it does not include development tools like the Java compiler.
Components of JRE
-
JVM
-
Core Java Libraries
-
Supporting Files
Purpose of JRE
-
Run Java programs
-
Provide Java libraries during execution
-
Support Java applications
Users who only want to run Java programs need the JRE (older Java versions).
Note: Since Java 11, Oracle no longer provides a separate JRE download. The JDK includes everything needed to develop and run Java applications.
What is JVM?
JVM stands for Java Virtual Machine.
The JVM is a virtual machine that executes Java bytecode.
It acts as a bridge between Java programs and the operating system.
Without the JVM, Java programs cannot run.
Responsibilities of JVM
-
Load bytecode
-
Verify bytecode
-
Convert bytecode into machine code
-
Execute the program
-
Manage memory
-
Perform Garbage Collection
-
Handle exceptions
Every operating system has its own JVM implementation, but all execute the same Java bytecode.
Relationship Between JDK, JRE, and JVM
Think of them like this:
JDK
-
Used to develop Java applications.
-
Contains the JRE and development tools.
JRE
-
Used to run Java applications.
-
Contains the JVM and Java libraries.
JVM
-
Executes Java bytecode.
-
Converts bytecode into machine code.
Simple Relationship
JDK
├── JRE
│ ├── JVM
│ └── Java Libraries
├── Java Compiler (javac)
├── Debugger
├── Documentation Tools
└── Development Utilities
Java Program Execution Process
Let’s understand the complete flow.
Step 1: Write the Source Code
Create a Java file:
public class Hello { public static void main(String[] args) { System.out.println("Hello Java"); } }
Save the file as:
Hello.java
Step 2: Compile the Program
Use the Java compiler:
javac Hello.java
The compiler checks for syntax errors.
If there are no errors, it creates:
Hello.class
This file contains Bytecode.
Step 3: Bytecode
Bytecode is an intermediate language understood by the JVM.
It is not machine code.
The same bytecode can run on Windows, Linux, or macOS because each platform has its own JVM.
Step 4: JVM Loads the Bytecode
Run the program:
java Hello
The JVM:
-
Loads the
.classfile. -
Verifies the bytecode.
-
Converts it into machine code using the Just-In-Time (JIT) compiler.
-
Executes the program.
Output:
Hello Java
Why is Java Platform Independent?
Java is platform independent because:
-
Java programs are compiled into Bytecode.
-
Bytecode is executed by the JVM.
-
Each operating system has its own JVM.
As a result, the same .class file can run on multiple platforms without modification.
This is why Java follows the principle:
Write Once, Run Anywhere (WORA).
Difference Between JDK, JRE, and JVM
| Feature | JDK | JRE | JVM |
|---|---|---|---|
| Full Form | Java Development Kit | Java Runtime Environment | Java Virtual Machine |
| Purpose | Develop and run Java programs | Run Java programs | Execute bytecode |
| Includes Compiler | Yes | No | No |
| Includes JVM | Yes | Yes | Itself |
| Includes Libraries | Yes | Yes | No |
| Used By | Developers | End Users | Java Runtime |
Key Points to Remember
-
JDK is used for developing Java applications.
-
JRE provides the environment required to run Java applications.
-
JVM executes Java bytecode.
-
JDK contains the JRE.
-
JRE contains the JVM.
-
Java programs are compiled into bytecode before execution.
-
Platform independence is achieved through the JVM.
Lesson Summary
In this lesson, you learned:
-
What JDK, JRE, and JVM are.
-
The purpose of each component.
-
How Java programs are compiled and executed.
-
Why Java is platform independent.
-
The relationship between JDK, JRE, and JVM.
Understanding these concepts is essential because they form the foundation of every Java application.
Practice Questions
-
What is JDK?
-
What is JRE?
-
What is JVM?
-
What is bytecode?
-
Which component compiles Java source code?
-
Which component executes Java bytecode?
-
Why is Java platform independent?
-
Explain the relationship between JDK, JRE, and JVM.
Assignment
-
Draw a diagram showing the relationship between JDK, JRE, and JVM.
-
Explain the Java program execution process in your own words.
-
Write the differences between JDK, JRE, and JVM in a table.
-
Explain why Java follows the “Write Once, Run Anywhere” principle.