Programming and Java Fundamentals

JDK, JRE, JVM, and Bytecode

ReadingPreview

আপনি একটি free preview lesson দেখছেন।

Lesson Overview

At the beginning of Java development, you will repeatedly encounter three terms:

  • JDK
  • JRE
  • JVM

Because these names are similar, they can easily be confusing. However, each has a different responsibility.

In this lesson, we will clearly understand:

  • What JDK is
  • What JRE is
  • What JVM is
  • What the javac and java commands do
  • The difference between compile time and runtime
  • How the components work together from Java source code to a running program

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 the previous lesson, we saw the simplified execution flow of a Java program:

Java Source Code (.java)
        ↓
Java Compiler
        ↓
Java Bytecode (.class)
        ↓
JVM
        ↓
Running Program

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.

The JDK includes:

  • Java compiler
  • Java launcher
  • Runtime components
  • Standard Java APIs
  • Development utilities

Simply put:

JDK is the Java developer's toolkit.

javac

javac is the Java compiler command.

Suppose your source file is:

Main.java

To compile it:

javac Main.java

If compilation is successful, the following file is created:

Main.class

That means:

Main.java
    ↓
javac
    ↓
Main.class

The Main.class file 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:

.class

In simplified terms, the JVM is responsible for:

  • Loading required classes
  • Executing bytecode
  • Managing runtime memory
  • Providing an execution environment for Java applications

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.

The same bytecode can run on different platforms when a compatible JVM is available.

                Windows JVM
              ↗
Main.class ─── Linux JVM
              ↘
                macOS JVM

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:

  • JVM
  • Java runtime libraries
  • Required runtime components

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.

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?

For a Java program:

Developer
    ↓
Main.java
    ↓
JDK's javac compiler
    ↓
Main.class
    ↓
Java runtime
    ↓
JVM
    ↓
Program Execution

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

There are two file types in Java development that should be clear from the beginning.

.java

Source code written by the developer.

Main.java

Example:

public class Main {

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

.class

Bytecode generated by the Java compiler.

Main.class

Relationship:

Main.java
    ↓
javac
    ↓
Main.class

Complete Compile and Run Example

Suppose we have:

public class Main {

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

The file is saved as:

Main.java

Compile:

javac Main.java

Generated file:

Main.class

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.

Example:

int age = "twenty";

Here, a String value is being assigned to an int variable.

The compiler will not accept this code.

Examples of compile-time problems include:

  • Invalid syntax
  • Incompatible type
  • Unknown variable
  • Invalid method usage

Runtime

Runtime is the stage when the program is executing.

Example:

int result = 10 / 0;

The code can compile, but it will fail during execution because of integer division by zero.

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:

  • Source code
  • JDK
  • IDE
  • Build tools
  • 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.

An IDE makes the development workflow easier by helping with:

  • Writing code
  • Highlighting errors
  • Invoking the compiler
  • Running applications
  • Debugging
  • Running 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.

The JVM executes compiled bytecode.

JDK and JVM Are the Same Thing

No.

JDK is the development toolkit.

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.

Important Terms

JDK

Java Development Kit. A collection of tools and runtime components used to develop and compile Java applications.

JRE

Java Runtime Environment. The concept of the runtime environment used to run Java applications.

JVM

Java Virtual Machine. The runtime engine that executes Java bytecode.

javac

The command used to compile Java source code.

java

The command used to start a compiled Java application.

Source Code

The .java code written by the developer.

Bytecode

A JVM-compatible intermediate instruction format produced by the Java compiler.

Compile Time

The stage when source code is compiled.

Runtime

The stage when the compiled program is executed.

Class File

A .class file containing Java bytecode.

Practice Exercise 1: Identify the Component

Match the following tasks with the most appropriate component:

  • JDK
  • JVM
  • 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

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:

Calculator.java

It contains:

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

In this lesson, we learned:

  • JDK is the Java development toolkit
  • JRE represents the Java runtime environment
  • JVM executes Java bytecode
  • javac compiles .java source code
  • The compiler generates .class bytecode
  • The java command starts a compiled application
  • Compile time and runtime are separate stages
  • Successful compilation does not guarantee runtime success
  • An IDE makes Java development easier but still depends on the underlying JDK