Three short abbreviations that sound interchangeable actually describe three different things. They are the program that you write, the machine that executes it, and the set of tools that produces it. Once you know which is which, it is obvious why some machines can run Java programs but cannot build them, and why a container image sometimes needs more than you expect.
The previous lesson in this module covered "How Do You Compile and Run Java Without an IDE?", and the next lesson in this module is about "What Is JShell and How Does It Speed Up Learning Java?", which shows you a prompt where you can try Java code without writing a class at all.
What is the JVM?
The Java Virtual Machine is the program that executes compiled Java code. Your source file is translated into bytecode, which is a set of instructions designed for one platform only. The JVM loads those instructions, checks them, and runs them, and while it runs them it translates them into the instructions of the machine that it happens to be running on.
That extra step is the whole point of the design. It is the reason a program that was compiled once runs on any machine with a compatible JVM. It is also the reason the JVM is allowed to do things that a native compiler cannot do, such as checking that an array index is inside the array before the access happens, and freeing memory that nothing can reach any more without the program asking for it.
What is the JRE?
The Java Runtime Environment is the JVM plus the standard class library. The standard class library is the large collection of ready made classes that give you collections, networking, file handling, dates and everything else that you would otherwise have to write yourself, together with the supporting machinery that the runtime needs.
Together they are the minimum that is needed to run a Java program and nothing more. A machine that has only the JRE can execute the Greeting.class file that you copied onto it, but it cannot compile your source code, because there is no compiler inside it.
What is the JDK?
The Java Development Kit is the JRE plus the development tools. Those tools are javac, javadoc, jshell, jar, jlink and the debugging tools. This is the package that a developer installs, and it is the package that a build server needs.
Here is how the pieces line up when you run a program.
Greeting.java --javac--> Greeting.class --java--> JVM --> your program runs
(JDK) (JRE = JVM + standard library)
Read the arrows as questions. Which tool turns your source code into bytecode? The compiler inside the JDK. Which thing executes the bytecode? The JVM. Which thing supplies the built in classes that your program uses? The standard library that ships with the runtime.
How can I see the difference for myself?
Compile a class and then look at the file that the compiler produced. The first four bytes of every class file are always the same, and reading them proves that the output really is a structured binary file and not text.
public class Magic {
public static void main(String[] args) throws Exception {
// The java.io package is always available, so no import is needed.
java.io.FileInputStream in = new java.io.FileInputStream(args[0]);
int first = in.read();
int second = in.read();
int third = in.read();
int fourth = in.read();
in.close();
System.out.println("first four bytes: " + first + " " + second + " " + third + " " + fourth);
boolean isClassFile = first == 0xCA && second == 0xFE && third == 0xBA && fourth == 0xBE;
System.out.println("looks like a class file: " + isClassFile);
}
}
Run it against a class that you compiled and the answer is true. What the JVM executes is not your source text. It is this binary form, and that is the entire reason the same bytecode can run on a server, on a laptop and on a phone.
What do I install where?
- On your own machine: the JDK. You need the compiler every single day, and the runtime comes with it anyway.
- In a build pipeline: the JDK, because the pipeline compiles the code before it tests it.
- In a container that only runs a finished build: a runtime is enough, although many build tools ask for a JDK anyway, so check what the tool expects.
- Never install a JRE and then expect javac to exist. That is the mistake that produces the "javac is not recognized" error on a machine where java works perfectly well.
Modern toolchains have blurred the line even further. Some distributions ship a single installer that only calls itself a JDK. Some builds bundle their own trimmed runtime, so the application does not care at all about what is installed on the host. The three concepts still describe what is really happening underneath, even when the packaging changes.
Can you see all three layers in one example?
Yes, and it is worth doing once so that the three names stop competing for the same space in your head. Write a small class, compile it, look at the result, and then run it. Each of those steps belongs to a different component, and the tool that you use tells you which one you are looking at.
# 1. the JDK compiles source code into bytecode
$ javac -d out src/com/example/Greeting.java
# 2. the result is a binary file and not text
$ file out/com/example/Greeting.class
out/com/example/Greeting.class: compiled Java class data, version 61.0
# 3. the JRE runs it
$ java -cp out com.example.Greeting
Hello from Java
# 4. and you can see the instructions that the JVM will run
$ javap -c -p -cp out com.example.Greeting
Step two is the one that surprises people, because a class file is binary and the utility file recognises it as such. Step four goes even further. The javap program takes the class apart and prints the bytecode instructions, which is a genuinely useful tool when you need to know what the compiler really produced.
// and the runtime can tell you which implementation loaded your code
public class Runtime {
public static void main(String[] args) {
System.out.println("vm name : " + System.getProperty("java.vm.name"));
System.out.println("vendor : " + System.getProperty("java.vm.vendor"));
String home = System.getProperty("java.home");
System.out.println("runtime folder holds a jre folder: "
+ new java.io.File(home, "jre").isDirectory());
}
}
The property java.home points at the runtime that is executing your code, and the answer to the folder question tells you which layout you are on. That single property is enough to answer the question "which runtime am I really using?" on a machine that has several of them installed, and that question comes up constantly in build logs, where the answer decides whether your build tested what you thought it tested.
Quick reference
- The JVM is the virtual machine that executes bytecode.
- The JRE is the JVM plus the standard class library. It is enough to run and not enough to build.
- The JDK is the JRE plus the development tools. This is what you install.
- Class files begin with the four bytes
CA FE BA BE. - The message "java works, javac does not" means that a runtime is installed where a JDK is needed.
Try it yourself: compile a class, run the magic number program against it, and then run it against a plain text file so that you can watch the check fail. Then find the java launcher and the javac compiler on your machine, note which folder each one lives in, and write down which of the three components each one belongs to.