Module 1 · Getting Started with Java — topic 2 of 3
JDK, JRE and JVM
The three pieces of the Java platform, what each one contains, how javac and java work together, and how to install and check your JDK.
Learning objectives
- Distinguish the JDK, JRE and JVM
- Describe the compile-then-run cycle
- Check which JDK version is installed
Definition
The JVM executes bytecode; the JRE is the JVM plus the standard class libraries needed to run programs; the JDK is the JRE plus development tools such as the javac compiler, needed to write programs.
Introduction
Beginners often hear three similar acronyms and are not sure which one to install. The answer is simple — install a JDK — but understanding what is inside it will help you read error messages and configure tools.
Think of it as three nested boxes. The innermost box, the JVM, runs programs. The middle box, the JRE, adds the libraries programs need. The outer box, the JDK, adds the tools developers need.
Why it matters
When a build fails with 'javac' is not recognized, or an IDE complains that no JDK is configured, you need to know which piece is missing.
Interviewers frequently ask about the difference between JDK, JRE and JVM because it shows whether you understand how Java really runs.
Detailed explanation
JVM (Java Virtual Machine). A program that loads .class files, verifies that the bytecode is safe, and executes it. It manages memory (the heap and the garbage collector) and uses a just-in-time compiler to turn frequently executed bytecode into native machine code. There are JVMs for every major operating system.
JRE (Java Runtime Environment). The JVM together with the standard class libraries — java.lang, java.util, java.io and so on. Historically you installed a JRE to *run* Java programs. Since Java 11, Oracle and most vendors ship only the JDK, and applications that need a runtime can bundle a trimmed one with the jlink tool.
JDK (Java Development Kit). The JRE plus development tools: javac (the compiler), java (the launcher), jar (packaging), javadoc (documentation), jshell (an interactive shell) and debugging and monitoring tools such as jdb and jconsole.
The cycle. You write Main.java, run javac Main.java to produce Main.class, then run java Main to start a JVM that loads and runs that class. Since Java 11 you can also run a single-file program directly with java Main.java; the launcher compiles it in memory first.
Free JDK builds. OpenJDK is open source, and several vendors publish free builds of it: Eclipse Temurin (Adoptium), Microsoft Build of OpenJDK, Amazon Corretto and Oracle's own OpenJDK builds. Any of them works for this course.
Syntax
- Use the same major version for
javacandjava. JAVA_HOMEshould point at the JDK folder so tools such as Maven and Gradle can find it.- A
.classfile compiled for Java 21 will not run on an older JVM.
Code examples
Asking the JVM about itself
Running on Java 17 or newer Processors available: at least 1
Runtime gives you information about the JVM your program is running in.
Bytecode is the same everywhere
This bytecode does not care which OS it runs on: true
More examples
Objects live on the heap managed by the JVM
Last value: 42, sum of all values: 42 Data size: 8 MB Fits in this JVM's heap: true The arrays can now be garbage collected
Step-by-step explanation
new int[200][10_000]asks the JVM for 200 arrays of 10,000 ints on the heap; underscores in numbers are only for readability.- Arrays of
intstart filled with zeros, so after storing 42 in one element the sum of all elements is 42. - Each
intisInteger.BYTES= 4 bytes, so the data is 200 x 10,000 x 4 = 8,000,000 bytes;200Lmakes the arithmetic uselong. Runtime.getRuntime().maxMemory()reports the largest heap this JVM may use, which is far more than 8 MB on any modern machine.- Setting
grid = nullremoves the only reference, so the garbage collector may reclaim the memory whenever it next runs — you never free it yourself.
Expected output
Running “Objects live on the heap managed by the JVM” prints exactly:
Practical applications
- Configuring
JAVA_HOMEfor Maven, Gradle and IDEs - Choosing a JDK version for a team project
- Tuning JVM memory with flags such as
-Xmx2gfor servers - Shipping a small custom runtime with
jlink
Best practices
- Install one LTS JDK and set
JAVA_HOMEto it. - Check
java -versionandjavac -versionagree before blaming your code. - Use
jshellto try out small expressions while learning.
Common mistakes
Running the .class file with its extension
Avoid
Prefer
java expects a class name, not a file name, so Main.class is read as a class called class in a package called Main.
Compiling with a newer JDK than the runtime
Avoid
Prefer
A newer class file version fails with UnsupportedClassVersionError on an older JVM.
Important notes
- Separate JRE downloads were discontinued after Java 8 by most vendors; just install the JDK.
- The JVM chooses its maximum heap from the machine's memory unless you set it with
-Xmx, so the exact limit varies between computers.
Key takeaways
- JVM runs bytecode; JRE = JVM + libraries; JDK = JRE + tools.
javaccompiles.javainto.class;javaruns a class.- Install a JDK (17 or 21) and point
JAVA_HOMEat it.
Try it yourself
- 1Run
java -versionandjavac -versionand write down the vendor and version. - 2Use
jshellto evaluateMath.sqrt(2)and"java".toUpperCase().
Finished reading? Mark it complete to update your dashboard.
Suggested practice
2 questions test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhich component contains the javac compiler?
- BeginnerHow do you run a compiled class Main.class from the terminal?