Skip to content

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.

Beginner 10 min read·Lesson 2 of 39 in Java Programming

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

Terminal
java -version # which runtime runs programs
javac -version # which compiler is installed (JDK only)
javac Main.java # compile: creates Main.class
java Main # run the class (no .class extension)
java Main.java # Java 11+: compile and run one file
jshell # try Java statements interactively
  • Use the same major version for javac and java.
  • JAVA_HOME should point at the JDK folder so tools such as Maven and Gradle can find it.
  • A .class file compiled for Java 21 will not run on an older JVM.

Code examples

Asking the JVM about itself

Java
public class Versions {
public static void main(String[] args) {
int major = Runtime.version().feature();
System.out.println("Running on Java " + (major >= 17 ? "17 or newer" : "an old version"));
System.out.println("Processors available: at least " + Math.min(1, Runtime.getRuntime().availableProcessors()));
}
}
Output
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

Java
public class Portable {
public static void main(String[] args) {
String os = System.getProperty("os.name");
boolean known = os != null && !os.isEmpty();
System.out.println("This bytecode does not care which OS it runs on: " + known);
}
}
Output
This bytecode does not care which OS it runs on: true

More examples

Objects live on the heap managed by the JVM

Java
public class Memory {
public static void main(String[] args) {
int[][] grid = new int[200][10_000]; // 200 arrays of 10,000 ints
grid[199][9_999] = 42;
long total = 0;
for (int[] row : grid)
for (int v : row) total += v;
long bytes = 200L * 10_000 * Integer.BYTES;
System.out.println("Last value: " + grid[199][9_999] + ", sum of all values: " + total);
System.out.println("Data size: " + bytes / 1_000_000 + " MB");
System.out.println("Fits in this JVM's heap: " + (Runtime.getRuntime().maxMemory() > bytes));
grid = null; // no references left: eligible for GC
System.out.println("The arrays can now be garbage collected");
}
}
Output
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 int start filled with zeros, so after storing 42 in one element the sum of all elements is 42.
  • Each int is Integer.BYTES = 4 bytes, so the data is 200 x 10,000 x 4 = 8,000,000 bytes; 200L makes the arithmetic use long.
  • Runtime.getRuntime().maxMemory() reports the largest heap this JVM may use, which is far more than 8 MB on any modern machine.
  • Setting grid = null removes 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:

TextOutput
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

Practical applications

  • Configuring JAVA_HOME for Maven, Gradle and IDEs
  • Choosing a JDK version for a team project
  • Tuning JVM memory with flags such as -Xmx2g for servers
  • Shipping a small custom runtime with jlink

Best practices

  • Install one LTS JDK and set JAVA_HOME to it.
  • Check java -version and javac -version agree before blaming your code.
  • Use jshell to try out small expressions while learning.

Common mistakes

Running the .class file with its extension

Avoid

java Main.class

Prefer

java Main

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

javac (JDK 21) then java (JRE 8)

Prefer

Use the same version, or compile with --release 8

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.
  • javac compiles .java into .class; java runs a class.
  • Install a JDK (17 or 21) and point JAVA_HOME at it.

Try it yourself

  1. 1
    Run java -version and javac -version and write down the vendor and version.
  2. 2
    Use jshell to evaluate Math.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?
Practise this topic