Before writing and executing a Java program, it is important to understand the environment in which Java operates.
Three terms appear repeatedly in almost every Java discussion: JDK, JRE, and JVM.
They are closely related, but they do not mean the same thing. Understanding their relationship helps us understand how Java programs are developed, compiled, executed, and managed.
Let's explore them from the ground up.
1. Why Does Java Have Three Different Components?
In many programming languages, the compiler, runtime environment, and execution engine are discussed separately. Java makes these distinctions particularly important.
Java was designed around an interesting philosophy:
Write once, run anywhere.
The idea was that developers should not need to rewrite their applications for different operating systems and hardware architectures.
To achieve this, Java introduced an intermediate representation called bytecode.
Instead of compiling Java source code directly into machine code for a particular processor, the Java compiler generates bytecode. The Java Virtual Machine then executes that bytecode on the target platform.
This architecture creates three important responsibilities:
- Development tools help programmers write, compile, debug, and package Java applications.
- The runtime environment provides the libraries and resources required to execute applications.
- The execution engine runs the compiled bytecode.
These responsibilities are associated with the JDK, JRE, and JVM.
2. What Is JVM (Java Virtual Machine)?
The JVM is the execution engine responsible for running Java bytecode.
It provides an environment in which compiled Java programs can execute independently of the underlying operating system and processor architecture.
Consider a simple Java program:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
When we compile this program:
javac HelloWorld.java
The compiler generates:
HelloWorld.class
This .class file contains Java bytecode.
The JVM loads this bytecode and executes it.
What exactly does the JVM do?
The JVM performs several important operations.
1. Class Loading
The Class Loader subsystem loads compiled .class files into memory when they are needed.
It helps locate and load the classes required by an application.
2. Bytecode Verification
Before execution, the JVM verifies bytecode to ensure that it follows important structural and safety rules.
For example, it checks that bytecode does not perform certain invalid operations that could compromise the execution environment.
3. Memory Management
The JVM manages memory used by Java applications.
Its runtime memory areas include:
- Heap: Stores objects and arrays.
- Java Stacks: Maintain method calls, local variables, and intermediate results.
- Method Area: Holds class-related structures and runtime metadata.
- Program Counter Registers: Track the current instruction for each thread.
- Native Method Stacks: Support execution of native methods.
4. Garbage Collection
Java developers generally do not need to manually deallocate objects.
The JVM includes garbage collection mechanisms that identify objects that are no longer reachable and reclaim their memory.
5. Bytecode Execution
The execution engine executes bytecode using interpretation and Just-In-Time (JIT) compilation.
The interpreter executes bytecode instructions, while the JIT compiler can compile frequently executed code into native machine code to improve performance.
This combination helps Java balance portability and execution speed.
Is JVM platform-independent?
The Java bytecode is designed to be platform-independent.
However, the JVM itself is platform-dependent.
For example:
- Windows has JVM implementations designed for Windows.
- Linux has JVM implementations designed for Linux.
- macOS has JVM implementations designed for macOS.
Each implementation understands the same Java bytecode specification but interacts with its underlying operating system differently.
This is what makes Java's portability possible.
3. What Is JRE (Java Runtime Environment)?
The JRE provides the environment required to run Java applications.
Conceptually, it includes:
- JVM
- Java class libraries
- Supporting runtime components and resources
The JVM executes bytecode, but Java applications also depend on standard libraries.
For example:
System.out.println("Hello, World!");
Here, System belongs to the Java standard library.
Similarly, Java applications may use libraries for:
- File handling
- Networking
- Collections
- Multithreading
- Input and output
- Date and time operations
The JRE provides these runtime libraries along with the JVM.
Think of the JVM as the engine and the JRE as the complete environment needed to operate that engine and run a Java application.
Does JRE compile Java programs?
Traditionally, a JRE does not include the Java compiler (javac).
Its primary purpose is application execution, not application development.
A developer compiles Java source code using the JDK, while an end user can run the resulting application using an appropriate Java runtime.
This separation allows development tools to remain independent of the environment required for ordinary application execution.
4. What Is JDK (Java Development Kit)?
The JDK is the complete development kit used to develop Java applications.
It includes the runtime components required to execute Java applications, along with tools required by developers.
Some important JDK tools include:
| Tool | Purpose |
|---|---|
javac |
Compiles Java source code into bytecode |
java |
Launches Java applications |
jar |
Creates and manages JAR files |
javadoc |
Generates documentation |
jdb |
Provides debugging capabilities |
jshell |
Interactive Java shell |
jlink |
Creates custom runtime images |
A developer typically installs the JDK to write, compile, test, debug, and package Java applications.
For example:
javac HelloWorld.java
java HelloWorld
The first command compiles the source code.
The second launches the application using the Java runtime.
Why does JDK contain the runtime?
Because developers need to execute their applications while developing them.
It would be inconvenient to install one package for development and another package for execution.
Therefore, the JDK provides the necessary runtime components together with development tools.
5. Understanding the Relationship Between JDK, JRE, and JVM
The traditional relationship can be represented as follows:
JDK
(Java Development Kit)
|
-----------------
| |
Development Runtime
Tools Environment
| |
javac JRE
javadoc |
jar JVM
jdb |
jshell Java Libraries
|
Bytecode
Execution
A simpler conceptual representation is:
JDK = Development Tools + Runtime Environment
JRE = JVM + Runtime Libraries
JVM = Bytecode Execution Engine
Remember that these are conceptual relationships. Modern Java distributions do not always package the JDK and JRE as separate nested directory structures.
6. What Happens When We Run a Java Program?
Let's connect these concepts using our HelloWorld example.
Step 1: Write the source code
We create a file:
HelloWorld.java
It contains human-readable Java code.
Step 2: Compile the source code
We execute:
javac HelloWorld.java
The Java compiler, provided by the JDK, generates:
HelloWorld.class
This file contains bytecode.
Step 3: Launch the application
We execute:
java HelloWorld
The Java launcher starts the runtime environment and invokes the JVM.
Step 4: Load and verify classes
The JVM loads the required classes, including application classes and standard Java library classes.
It verifies the bytecode before execution.
Step 5: Execute the bytecode
The JVM execution engine interprets bytecode and may use JIT compilation to optimize frequently executed code.
Finally, the program produces:
Hello, World!
The complete journey looks like this:
Java Source Code
|
v
javac Compiler
|
v
Bytecode (.class)
|
v
Java Launcher
|
v
JVM
|
v
Class Loading & Verification
|
v
Interpreter / JIT Compiler
|
v
Operating System
|
v
Output
This is the basic execution journey of a Java application.
7. A Modern Java Perspective: What Changed After Java 9?
There is one important historical detail worth understanding.
Traditionally, Java distributions were commonly provided in two forms:
- JDK: For developers.
- JRE: For running Java applications.
Developers installed the JDK, while users who only needed to execute Java applications could install the JRE.
However, Java's packaging model evolved significantly with Java 9.
The introduction of the Java Platform Module System
Java 9 introduced the Java Platform Module System (JPMS).
Instead of treating the Java platform as one large collection of libraries, Java introduced modules to organize platform components more explicitly.
For example:
java.base: Fundamental Java functionality.java.sql: Database connectivity APIs.java.xml: XML processing APIs.java.desktop: Desktop application APIs.
This modular architecture made it possible to build smaller custom runtime environments.
What happened to the standalone JRE?
Starting with Java 11, Oracle stopped offering a separate traditional JRE download as part of its standard JDK distribution.
This does not mean that Java applications no longer need a runtime.
It means that the way runtime environments are packaged and distributed has evolved.
Today, developers can create custom runtime images using:
jlink
For example, an application that needs only a subset of Java modules can be distributed with a smaller custom runtime.
Different Java vendors may also provide different runtime distributions.
Therefore, the traditional JDK-JRE-JVM relationship remains useful for understanding Java architecture, but the actual packaging can differ depending on the Java version and distribution.
8. Can We Run Java Without Installing the JDK?
Yes.
A Java application that has already been compiled can run without the full JDK, provided an appropriate Java runtime environment is available.
For example, if someone gives us a compiled Java application, we do not necessarily need the compiler to execute it.
We need the runtime components required by that application.
This is particularly useful in production environments.
A development machine may contain:
- JDK
- Maven or Gradle
- IDE
- Testing tools
- Debugging tools
A production container may contain only:
- Application JAR
- Required runtime
- Configuration
- Other application dependencies
This helps reduce the size and attack surface of production deployments.
9. Which One Should a Java Developer Install?
For most Java developers, the answer is straightforward: install a JDK.
You will need it to:
- Compile Java programs.
- Run Java applications.
- Work with Maven or Gradle.
- Develop Spring Boot applications.
- Debug and troubleshoot applications.
- Use Java development tools.
Some popular JDK distributions include:
- Oracle JDK
- Eclipse Temurin
- Amazon Corretto
- Azul Zulu
- Microsoft Build of OpenJDK
They are different distributions of Java built around the OpenJDK ecosystem or related implementations.
The right choice depends on licensing, support, compatibility, organizational standards, and deployment requirements.
10. Final Thoughts
JDK, JRE, and JVM are not three competing technologies. They represent different layers of the Java ecosystem.
The JDK provides the tools to build Java applications.
The JRE represents the environment needed to run them.
The JVM executes the bytecode and manages the underlying execution process.
Understanding these distinctions gives us a clearer picture of what happens between writing a Java program and seeing its output.
And this is only the beginning.
Java is not simply a programming language. It is an ecosystem consisting of a language specification, compiler, bytecode format, virtual machine, runtime libraries, development tools, and a growing collection of frameworks.
In the next article, we will move one layer deeper and explore the structure of a Java program itself - classes, objects, methods, variables, packages, and the role of the main() method.
Key Takeaways
- JDK: Provides tools to develop, compile, debug, and package Java applications.
- JRE: Represents the runtime environment required to execute Java applications.
- JVM: Loads, verifies, and executes Java bytecode.
- Bytecode: Intermediate representation generated by the Java compiler.
- JIT Compiler: Optimizes frequently executed bytecode by compiling it into native machine code.
- Modern Java: Modular runtime images and vendor-specific distributions have changed how Java runtimes are packaged.
The simplest way to remember:
The JDK builds the application. The JRE provides its runtime environment. The JVM executes it.