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.
আপনি একটি free preview lesson দেখছেন।
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
javacandjavacommands do - Identify
.javaand.classfiles - 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:
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:
java Main
Although the Main.class file exists, you do not include .class in the command.
Incorrect:
java Main.class
Correct:
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:
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:
| Component | Main Responsibility |
|---|---|
| JDK | Develop and compile Java applications |
| JRE | The concept of the Java runtime environment |
| JVM | Execute 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:
- Install a JDK
- Write Java source code
- Compile the code using
javac - Generate
.classbytecode - Start the application using the
javacommand - 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:
public class Main {
public static void main(String[] args) {
System.out.println("Learning Java");
}
}
Compile it, which generates Main.class:
javac Main.java
Run:
java Main
Output:
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:
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:
int result = 10 / 0;
That means:
Successful compilation does not guarantee that a program will successfully run at runtime.
Compile Time vs. Runtime
| Topic | Compile Time | Runtime |
|---|---|---|
| When | While code is being compiled | While the program is running |
| Main component | Compiler | JVM |
| Input | Source code | Compiled bytecode |
| Example command | javac Main.java | java Main |
| Common problem | Syntax/type error | Runtime 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.
- Compile Java source code
- Provide Java development tools
- Execute bytecode
- Provide the
javaccommand
Practice Exercise 2: Compile Time or Runtime?
Write which stage each problem occurs in.
Problem 1
int age = "twenty";
Problem 2
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:
public class Calculator {
}
Write:
- The compile command
- The name of the generated file
- 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
javaccompiler, thejavalauncher and the runtime components. -
javac Main.javacompiles source code into bytecode inMain.class;java Mainstarts the application, without.classin the command. -
The JVM executes bytecode, not
.javasource 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.
আগে ভাবুন
শীঘ্রই আসছেCheckpoint প্রশ্ন, যেখানে lesson উত্তর দেখানোর আগে আপনাকে একটি উত্তর বেছে নিতে হবে।