Õppetund 4/140
Kirjalik õppetund · 9 min

JDK, JRE, JVM, and Bytecode

Separate the jobs of the JDK, JRE and JVM, and use `javac` and `java` to move from source code at compile time to a running program.

Eelvaade

Vaatad tasuta eelvaate õppetundi.

Lesson Overview

At the beginning of Java development, you will repeatedly encounter three terms: JDK, JRE, and JVM. Because these names are similar, they can easily be confusing. However, each has a different responsibility, and mixing them up causes real problems: installing something that cannot compile code, or expecting the IDE to work without a JDK. In this lesson, we take the flow from lesson 1.02 and put a name to each part of it, so that you know exactly what you install in the next lesson and what each command does.

Learning Objectives

After completing this lesson, you will be able to:

  • Distinguish the responsibilities of JDK, JRE, and JVM
  • Understand what the javac and java commands do
  • Identify .java and .class files
  • Explain the difference between compile time and runtime
  • Understand the basic relationship between the Java development environment and runtime environment

JDK, JRE, and JVM

In lesson 1.02, we followed a Java program from source code (.java) through the Java compiler to bytecode (.class), which the JVM runs. Now let's look at the components in this flow more clearly.

What Is JDK?

JDK — Java Development Kit is a collection of tools required to develop Java applications. As a developer, you will generally install a JDK to compile and run Java code. It includes the Java compiler, the Java launcher, the runtime components, the standard Java APIs and other development utilities. Simply put:

JDK is the Java developer's toolkit.

javac

javac is the Java compiler command. If your source file is Main.java, you compile it with:

bash
javac Main.java

If compilation is successful, the file Main.class is created next to it. It contains Java bytecode.

java

The java command starts a compiled Java application:

bash
java Main

Although the Main.class file exists, you do not include .class in the command.

Incorrect:

bash
java Main.class

Correct:

bash
java Main

The java launcher starts the required runtime and executes the Main class through the JVM.

What Is JVM?

JVM — Java Virtual Machine is the runtime engine that executes Java bytecode. The JVM does not directly execute .java source code; its input is compiled bytecode in .class files.

In simplified terms, the JVM loads the classes a program needs, executes their bytecode, manages runtime memory and provides the environment Java applications run in. We will explore JVM class loading, garbage collection, JIT compilation, and memory internals in later lessons when needed.

Why Is the JVM Important?

Instead of producing a platform-specific executable, the Java compiler produces JVM-compatible bytecode. As lesson 1.02 showed, the same Main.class can run on Windows, Linux and macOS when a compatible JVM is available. Bytecode can be portable, but the JVM implementation differs for each operating system.

What Is JRE?

JRE — Java Runtime Environment refers to the runtime environment required to run Java applications. Conceptually, it includes the JVM, the Java runtime libraries and the other components needed at runtime. Simply put:

The purpose of the JRE is to run Java applications.

Traditionally, the distinction was:

text
JDK → Develop + Compile + Run
JRE → Run
JVM → Execute Bytecode

In the modern Java ecosystem, standalone JRE installations are not as central as they once were. Developers generally install a full JDK, which includes the runtime components required to run applications.

That is what we will do in the next lesson. The JDK we install, OpenJDK 21 (the Eclipse Temurin build), already contains the runtime (the JVM and the class libraries), so you do not need a separate JRE for this course. Adoptium, the project that publishes Temurin, also offers a runtime-only Temurin JRE 21 package for machines that only run Java applications. It has no javac, so when the download page asks for a package type, we choose JDK, not JRE.

As a beginner, the most important distinction is:

ComponentMain Responsibility
JDKDevelop and compile Java applications
JREThe concept of the Java runtime environment
JVMExecute Java bytecode

How Do JDK, JRE, and JVM Work Together?

The JDK's javac turns Main.java into Main.class, and the Java runtime (also part of the JDK) starts a JVM that executes it. From a developer's perspective, the common workflow is:

  1. Install a JDK
  2. Write Java source code
  3. Compile the code using javac
  4. Generate .class bytecode
  5. Start the application using the java command
  6. JVM executes the bytecode

Source File and Class File

Two file types should be clear from the beginning. A .java file, such as Main.java, contains source code written by the developer. A .class file, such as Main.class, contains bytecode generated by the Java compiler. You edit the first; the compiler creates the second, and you never edit it by hand.

Complete Compile and Run Example

Suppose we have this program, saved as Main.java:

java
public class Main {

    public static void main(String[] args) {
        System.out.println("Learning Java");
    }
}

Compile it, which generates Main.class:

bash
javac Main.java

Run:

bash
java Main

Output:

text
Learning Java

This single example shows the entire basic workflow: source code, compile, bytecode, run, output.

Compile Time and Runtime

In Java development, compile time and runtime are two separate stages.

Compile Time

Compile time is the stage when source code is being compiled. At this stage, the compiler analyzes the source code. Here, a String value is being assigned to an int variable, so the compiler will not accept this code:

java
int age = "twenty";

Other compile-time problems include invalid syntax, using a variable that was never declared, and calling a method in a way it does not allow.

Runtime

Runtime is the stage when the program is executing. The following code can compile, but it will fail during execution because of integer division by zero:

java
int result = 10 / 0;

That means:

Successful compilation does not guarantee that a program will successfully run at runtime.

Compile Time vs. Runtime

TopicCompile TimeRuntime
WhenWhile code is being compiledWhile the program is running
Main componentCompilerJVM
InputSource codeCompiled bytecode
Example commandjavac Main.javajava Main
Common problemSyntax/type errorRuntime exception

Development Environment and Runtime Environment

A developer's computer generally contains the source code, a JDK, an IDE, build tools and tests. In this environment, code is written, compiled, and tested. In a production environment, the application's source code is not necessarily required. The application can run as long as the compiled application and the required Java runtime are available. This distinction will become important later when we learn about backend deployment.

Why Is JDK Needed Even If You Have an IDE?

An IDE such as IntelliJ IDEA does not compile or execute the Java language by itself. It makes the development workflow easier: it helps you write code, highlights errors, invokes the compiler, runs and debugs applications, and runs tests. However, the underlying Java compiler and runtime come from the JDK. That is why a JDK must be configured alongside the IDE when setting up a Java development environment.

Common Misconceptions

JVM Compiles Source Code

No. The javac compiler compiles source code, and the JVM executes compiled bytecode.

JDK and JVM Are the Same Thing

No. JDK is the development toolkit, while JVM is the runtime engine that executes Java bytecode.

Bytecode Is Machine Code

No. Bytecode is an intermediate instruction format for the JVM.

Successful Compilation Means the Program Is Correct

No. A program may fail at runtime or may run successfully but still produce an incorrect result because of a logical error.

You Do Not Need JDK If You Have an IDE

No. An IDE depends on a configured JDK to use the Java compiler and runtime.

Practice Exercise 1: Identify the Component

Solutions: lesson 1.22.

Match the following tasks with the most appropriate component: JDK, JVM, or Java compiler.

  1. Compile Java source code
  2. Provide Java development tools
  3. Execute bytecode
  4. Provide the javac command

Practice Exercise 2: Compile Time or Runtime?

Write which stage each problem occurs in.

Problem 1

java
int age = "twenty";

Problem 2

java
int result = 10 / 0;

Problem 3

An undeclared variable is used.

Problem 4

At runtime, the program tries to open a file that does not exist.

Practice Exercise 3: Write the Commands

Suppose you have a file Calculator.java that contains:

java
public class Calculator {
}

Write:

  1. The compile command
  2. The name of the generated file
  3. The run command

Knowledge Check

Question 1

What is the main purpose of the JDK?

Question 2

What does the JVM execute?

Question 3

What is the difference between the javac and java commands?

Question 4

What is the difference between .java and .class files?

Question 5

What is the difference between compile time and runtime?

Question 6

What does JRE conceptually mean?

Question 7

Why is a JDK still needed when using an IDE?

Knowledge Check Answers

Answer 1

The JDK provides the tools and runtime components required to develop, compile, and run Java applications.

Answer 2

The JVM executes compiled Java bytecode.

Answer 3

javac compiles Java source code. java starts a compiled Java application.

Answer 4

A .java file contains source code written by the developer. A .class file contains Java bytecode produced by the compiler.

Answer 5

At compile time, the compiler analyzes and translates the source code. At runtime, the compiled program is executed through the JVM.

Answer 6

JRE refers to the environment containing the JVM, runtime libraries, and supporting components required to run Java applications.

Answer 7

An IDE manages the development workflow, but it uses the compiler and runtime from a configured JDK to compile and execute Java code.

Lesson Summary

  • The JDK is the Java developer's toolkit. It includes the javac compiler, the java launcher and the runtime components.

  • javac Main.java compiles source code into bytecode in Main.class; java Main starts the application, without .class in the command.

  • The JVM executes bytecode, not .java source code. Bytecode can be portable, but each operating system needs its own JVM.

  • The JRE is the concept of the runtime environment needed to run Java applications. Today, developers generally install a full JDK.

  • Compile time is when the compiler checks and translates source code. Runtime is when the JVM executes the program.

  • Successful compilation does not guarantee that a program will run successfully.

  • An IDE such as IntelliJ IDEA still uses the compiler and runtime from a configured JDK.

Mõtle enne

Tulekul

Kontrollküsimused, mille puhul valid vastuse enne, kui õppetund selle näitab.