JDK, JRE and JVM: how Java code runs
The three names every Java developer must know, what each one does, and the path your code takes from a .java file to a running program.
You write Java in a text file. Your computer can't run that text directly. Three tools work together to turn it into a running program: the JDK, the JRE and the JVM. Let's meet them one at a time.
01The short version
- JVM (Java Virtual Machine): the engine that runs Java programs.
- JRE (Java Runtime Environment): the JVM plus the standard libraries a program needs to run.
- JDK (Java Development Kit): the JRE plus the tools you need to build programs, such as the compiler
javac.
Think of it like a kitchen. The JVM is the stove, the JRE is the stove with pots and ingredients, and the JDK is the whole kitchen, including the knives and recipe book you need to cook something new.
02From your code to a running program
Java code goes through two steps before it runs:
- Compile: the compiler
javacturns your.javafile into bytecode, saved in a.classfile. Bytecode isn't machine code for any real processor. It's a simple, portable instruction set for the JVM. - Run: the
javacommand starts a JVM, which loads the.classfile and runs the bytecode.
In the terminal it looks like this:
javac Hello.java # compile: creates Hello.class
java Hello # run: the JVM runs the bytecodeHello, Java!
03Write once, run anywhere
Because javac produces bytecode and not machine code, the same .class file runs on Windows, macOS and Linux. Each operating system has its own JVM that understands the same bytecode. This idea is called "write once, run anywhere", and it's one of the main reasons Java became popular.
04Is Java compiled or interpreted?
Both. The code is compiled to bytecode first. Then the JVM starts by interpreting the bytecode, and, while the program runs, its JIT compiler (Just-In-Time) turns the parts that run most often into fast native machine code. That's why long-running Java programs get faster after they warm up.
05In the interview
Common follow-up questions:
- Why is Java platform independent? Because
javacproduces bytecode, and every operating system has a JVM that runs the same bytecode. - Is the JVM platform independent? No. There's a different JVM for each operating system, and that's what lets bytecode stay the same.
- What does the JIT compiler do? It compiles frequently used bytecode into native machine code while the program runs, so hot code runs much faster.
06Check your understanding
Quiz · 4 questions
0 of 4 answered
1. Which one do you install to write and compile Java programs?
2. What does javac produce from Hello.java?
3. Why can the same Hello.class run on Windows and Linux?
4. What makes a long-running Java program faster over time?