0% completed
JDK, JRE, and JVM
On This Page
What is the difference between the JDK, the JRE, and the JVM?
What does platform independent mean for Java?
Which memory areas and parts does a running JVM have?
How do HotSpot, OpenJDK, and vendor builds such as Temurin relate?
Predict the Output
Key Takeaways
The folder of an installed JDK 21 holds several hundred files. Four of them show how Java is organized:
bin/javaccompiles Java source files.bin/javastarts a Java program.lib/modulescontains the standard library, such asStringandArrayList.jvm.dllon Windows, orlibjvm.soon Linux, is the Java Virtual Machine itself.
A finished program needs the last three files and not the first. The answers below name the layers behind these files, the parts of Java that are portable, the content of a running JVM, and the relation between products such as OpenJDK and Temurin.
What is the difference between the JDK, the JRE, and the JVM?
Level: Junior
The JVM runs compiled Java code, the JRE is the JVM together with the standard library, and the JDK is all of this plus the development tools. Each layer therefore contains the one before it.
The Java Virtual Machine (JVM) is a program that reads class files, which are the output of the compiler, and executes the instructions inside them. The Java Runtime Environment (JRE) is everything that a compiled program needs in order to run. The Java Development Kit (JDK) adds the compiler javac and tools such as jar, javap, and jcmd.
| Part | Contains | Who needs it |
|---|---|---|
| JVM | The program that executes class files | Every running Java program |
| JRE | The JVM and the standard library | A machine that runs Java programs |
| JDK | The JRE contents and the development tools | A machine that builds Java programs |
Oracle stopped offering a separate JRE with Java 11. Some other vendors still build one, and the word remains in use for the runtime part of a JDK.
Since Java 9, the JDK also includes jlink, a tool that builds a smaller runtime for one application. The standard library is divided into modules, which are named groups of packages, and the JDK 21 used here contains 70 of them. The commands below build a runtime that contains a single module and then list its content.
jlink --add-modules java.base --output rt rt/bin/java --list-modules
java.base@21.0.12.1
The folder rt holds a JVM and the module java.base, with no javac. It took 49 MB on the machine where these commands ran, while the JDK took 329 MB.
Notice that the runtime on the right has the same two inner layers as the JDK. It has fewer modules and no development tools.
Common mistake: Saying that a server must have a JDK. A compiled program runs on a runtime alone. A JDK on the server is still useful for diagnosis, because tools such as jcmd and jstack belong to it.
Follow-up questions
1. What happens when a program uses a class from a module that its runtime does not contain?
<details><summary>Show answer</summary>A program on the class path starts, and it fails with NoClassDefFoundError when it first needs the class. A program that uses java.sql.Timestamp stops in this way on the runtime built above. The command jdeps --print-module-deps App.class prints the modules that a class file needs, here java.base,java.sql. A runtime built with both modules runs the program correctly.
2. Does a JDK contain a separate JRE folder?
<details><summary>Show answer</summary>Not since Java 9. In Java 8, the JDK contained a folder named jre, and the library was stored in a file named rt.jar. Java 9 removed both, and a JDK now has one runtime in its own bin and lib folders.
What does platform independent mean for Java?
Level: Junior
It means that the same class file runs on every operating system and processor for which a compatible JVM exists. The compiled code is portable. The JVM is not portable, because each platform needs its own build of it.
A C compiler produces machine code, the instructions of one processor type, stored in the file format of one operating system. The compiler javac produces bytecode instead, which is the instruction set of the JVM. A class file is the file that holds the bytecode of one class. The JVM for Windows and the JVM for Linux both read the same class file, and each one executes it on its own machine.
Two rules make the results equal as well. The language fixes the size of each numeric primitive type, so an int has 32 bits on every machine. The format of a class file is fixed too, including the order of the bytes. The program below reads the start of its own class file and then builds a file path without naming a separator.
First four bytes: CAFEBABE Names in the path: 2 File name: orders.txt %n is the system line separator: true
Every class file starts with the four bytes CAFEBABE, which identify the format. The remaining lines are the same on Windows and Linux, because the library supplies the separators of the current system at run time.
A program can still lose its portability in these ways:
- It contains a file path in the form of one system, such as
C:\data\orders.txt. - It assumes that
Orders.txtandorders.txtare the same file, which depends on the file system. - It starts an external command or loads a library that exists on one system.
Look at the single class file in the middle. One compilation is enough, while three different JVM builds execute the result.
Common mistake: Saying that the JVM is platform independent. The JVM is the part that depends on the platform, and this dependence allows the class files to be independent of it.
Follow-up questions
1. Can the JVM run programs written in a language other than Java?
<details><summary>Show answer</summary>Yes, because the JVM reads class files and has no knowledge of the Java language. Any compiler that produces valid class files can target it, and the compilers of Kotlin, Scala, and Groovy do so.
</details>2. How does a native method affect portability?
<details><summary>Show answer</summary>It ties the program to the platforms for which its native library was built. A native method is declared in Java with the keyword native and implemented in another language, usually C or C++. The library must be compiled separately for each combination of operating system and processor. The Java part stays portable.
Which memory areas and parts does a running JVM have?
Level: Mid-level
A running JVM has a class loading part, a set of memory areas, and an execution engine that runs the bytecode. The heap and the method area are shared by all threads. Every thread also has its own stack and its own program counter.
The class loading part finds and prepares classes. The execution engine contains an interpreter, which executes bytecode instructions one by one. In HotSpot, the JVM in most builds, it also contains compilers that translate frequently used methods to machine code while the program runs. The garbage collector frees the memory of objects that the program can no longer reach.
The table lists the main memory areas of the JVM Specification:
| Area | One for | Holds |
|---|---|---|
| Heap | The whole JVM | Objects and arrays |
| Method area | The whole JVM | Class data, such as the code of each method |
| Stack | Each thread | One frame for each method call in progress |
| Program counter | Each thread | The address of the current bytecode instruction |
| Native method stack | Each thread | Calls to native methods, in JVMs that keep it separate |
A frame is the block of memory for one method call, and it holds the local variables of that call. In HotSpot, class data is stored in an area named metaspace, outside the heap.
The program below starts two threads that share one Stock object.
Ana: quantity = 10, remaining = 20 Ben: quantity = 5, remaining = 15 main: bananas = 15
Both threads run the same method, yet the values of quantity differ, because each call has a frame on the stack of its own thread. The second thread starts from 20 bananas, so it sees the change that the first thread made to the object on the heap.
Common mistake: Saying that every object reference is stored on the stack. A local variable that holds a reference is in a frame, but a reference in a field is stored inside its object, on the heap.
Follow-up questions
1. Where are static fields stored?
<details><summary>Show answer</summary>In current releases of HotSpot, static fields are stored on the heap, together with the Class object of their class. HotSpot once kept class data and static fields in an area called the permanent generation. Java 8 removed that area and moved the class data to metaspace.
2. Which memory areas does the garbage collector clean?
<details><summary>Show answer</summary>Its main work is the heap, where it frees unreachable objects. A stack needs no collector, because a frame is removed when its method call ends. Class data in metaspace is freed when its class loader is no longer reachable.
</details>How do HotSpot, OpenJDK, and vendor builds such as Temurin relate?
Level: Mid-level
Java is a set of written specifications, OpenJDK is the open-source project that implements them, HotSpot is the JVM inside OpenJDK, and Temurin is one of the builds made from that source code. Temurin is the product that you install, and it contains HotSpot in compiled form.
Three terms describe the chain:
- Specification. The Java Language Specification defines the language. The JVM Specification defines the class file format and the behavior of every bytecode instruction.
- Implementation. The OpenJDK project contains the source code of the compiler, the standard library, the tools, and HotSpot. HotSpot is written mostly in C and C++.
- Build. A vendor compiles this source code for a list of platforms, tests it, and publishes the result. Eclipse Temurin, Amazon Corretto, and Oracle JDK are builds of this kind.
The command java -version prints these names. Its first line shows the version number and a date, and it is cut here.
OpenJDK Runtime Environment Temurin-21.0.12.1+1 (build 21.0.12.1+1-LTS) OpenJDK 64-Bit Server VM Temurin-21.0.12.1+1 (build 21.0.12.1+1-LTS, mixed mode, sharing)
The runtime and the virtual machine both come from OpenJDK, and Temurin is the name of the build. The words "mixed mode" mean that the JVM interprets code and also compiles it.
A specification leaves many choices open. The JVM Specification does not prescribe the memory layout, the garbage collection algorithm, or the translation to machine code. Eclipse OpenJ9, for example, is a different JVM that also runs with the OpenJDK library.
Common mistake: Treating HotSpot behavior as a rule of Java. Metaspace, the list of garbage collectors, and the options that start with -XX belong to one implementation, and another JVM may not have them.
Follow-up questions
1. What does it mean that a build is TCK certified?
<details><summary>Show answer</summary>It means that the build passed the Technology Compatibility Kit, the test suite for the Java SE specification. The tests show that the compiler, the library, and the JVM behave as specified. They do not measure speed or memory use, so two certified builds can still perform differently.
</details>2. Do two builds of the same OpenJDK version run the same program differently?
<details><summary>Show answer</summary>A correct program produces the same specified results on both, because both follow the same specifications. Differences remain outside the specification. A vendor can include a garbage collector that another vendor leaves out. Vendors also support each version for different lengths of time.
</details>Predict the Output
The program below prints facts about primitive types. Write down its six lines, and decide whether any of them could change on another machine.
The program prints 4 8 2, -2147483648, 65535, -56, -3, and 2147483647, and no line can change on another machine. An int has 4 bytes, a long has 8, and a char has 2. Adding 1 to the largest int value exceeds its 32 bits, so the result is the smallest int value. The largest char value is 65535, because a char is an unsigned 16-bit number. A byte holds values from -128 to 127, so 200 becomes 200 - 256, which is -56. Integer division rounds toward zero, so -3.5 becomes -3. Three billion is too large for an int, and the conversion from double returns the largest int value.
Key Takeaways
- The JDK contains the development tools and a runtime, and the runtime contains the JVM and the standard library.
- Oracle offers no separate JRE since Java 11, and
jlinkbuilds a runtime with the modules that one application needs. - Class files are portable, while every platform needs its own JVM.
- The heap and the method area are shared, and each thread has its own stack and program counter.
- OpenJDK is the source code, HotSpot is its JVM, and Temurin is one of several builds.
The compiler and the JVM appeared above as two separate programs with a class file between them. The lesson From Source Code to a Running Program opens such a file and follows one method from its source text to machine code.
Reading Progress
0%
On This Page
What is the difference between the JDK, the JRE, and the JVM?
What does platform independent mean for Java?
Which memory areas and parts does a running JVM have?
How do HotSpot, OpenJDK, and vendor builds such as Temurin relate?
Predict the Output
Key Takeaways