When I started learning Java, three words kept confusing me: JDK, JRE and JVM. They sound alike, people use them as if they mean the same thing, and most tutorials just say "install the JDK" without explaining why.
Once I understood the journey your code takes, from the moment you type it to the moment it runs, everything made sense. Let's walk through it step by step.
Step 1: You write Java code
It starts with you, the developer. You write code in a plain text file that ends with .java.
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
This file is for humans. Your computer cannot run it directly, because processors don't understand Java. They only understand machine code, which is just 0s and 1s.
Step 2: The compiler turns it into bytecode
Next, you run the Java compiler, called javac.
javac Hello.java
The compiler reads your code and creates a new file called Hello.class. This file contains bytecode.
Bytecode is not the final machine code. It's a middle form: simpler than your Java code, but not tied to any particular computer. Hold on to that last part, because it matters a lot later.
Step 3: The JVM runs the bytecode
Now the JVM (Java Virtual Machine) comes in. Think of it as a small computer that lives inside your real computer. It reads the bytecode and runs it, translating it into instructions your actual processor understands.
When you run this:
java Hello
the JVM looks for one specific starting point:
public static void main(String[] args)
That's why every Java program needs this exact line. It's the front door. If the JVM can't find it, it doesn't know where to start.
Step 4: JRE gives the JVM its tools
The JVM can't work alone. Your program probably prints text, reads files, uses lists and so on. All of that comes from ready-made code called libraries.
So we put the two together:
JRE (Java Runtime Environment) = JVM + libraries
The JRE is everything you need to run a Java program. If you only want to use a Java app and not build one, the JRE is enough.
Step 5: JDK is for people who build things
Now what if you want to write Java programs? You need the compiler (javac), a debugger and other developer tools. These come in the JDK (Java Development Kit).
JDK = JRE + development tools
The whole picture
Here is the nesting. Each one sits inside the next:
+------------------------------------------+
| |
| +----------------------------------+ |
| | | |
| | +----------------+ | |
| | | JVM | | |
| | +----------------+ | |
| | | |
| | JRE | |
| +----------------------------------+ |
| |
+------------------------------------------+
JDK
A simple way to remember it:
| Name | What it is | When you need it |
|---|---|---|
| JVM | The engine that runs bytecode | Always (it's inside the others) |
| JRE | JVM + libraries | To run Java programs |
| JDK | JRE + compiler and tools | To write Java programs |
If you're a developer, install the JDK. It already includes the other two.
Where the OS and hardware fit in
Below all of this sits your operating system (Windows, Linux or macOS) and then the hardware.
Your Java code (.java)
|
v
javac compiler
|
v
Bytecode (.class)
|
v
+---------------------+
| JRE (JVM + libs) |
+---------------------+
| OS | <- Windows / Linux / macOS
+---------------------+
| HW |
+---------------------+
Your Java program never talks to the OS directly. It talks to the JVM, and the JVM talks to the OS. This is the secret behind the big idea of Java.
The big idea: WORA
You'll hear the short form WORA, which means Write Once, Run Anywhere.
Why does this work? Think about the alternative. In a language like C, the compiler turns your code straight into machine code for one specific system. A program compiled for Windows won't run on Linux, so you have to compile it again for each one.
Java takes a different path:
- You compile once into bytecode, which belongs to no particular OS.
- Each operating system has its own JVM, built specially for it.
- Every JVM understands the same bytecode.
So the same .class file runs on Windows, Linux and macOS without any change. Only the JVM differs from system to system, and the JVM is the one that knows how to talk to its own OS. You write the code one time, and the JVM handles the differences.
There is a small trade-off. Running through a JVM adds a layer, so it can be a little slower than code compiled straight for one machine. In practice, modern JVMs are very well optimized, and for most programs the freedom to run anywhere is worth far more than the small cost.
Quick recap
- You write
.javacode. -
javaccompiles it into.classbytecode. - The JVM runs the bytecode, starting from the
mainmethod. - The JRE is the JVM plus libraries, so you can run programs.
- The JDK is the JRE plus developer tools, so you can build programs.
- WORA works because bytecode is the same everywhere, and each OS has its own JVM to run it.
Once you see it as one journey (code → bytecode → JVM → OS → hardware), the three confusing names stop being confusing. They are just layers, each one wrapped around the one before it.
If this helped, or if I got something wrong, let me know in the comments. I'm still learning too. 🙂
Top comments (0)