Java Environment Tutorial: JDK, JRE, JVM & Java Commands
Introduction
Before writing Java applications, it is essential to understand the Java Environment. Many beginners get confused by terms like JDK, JRE, and JVM, but these are the foundation of every Java program.
When you write a Java program, it goes through several stages before it runs on your computer. The source code is compiled into bytecode, and the Java Virtual Machine (JVM) executes that bytecode. This process allows Java to run on different operating systems without changing the source code.
In this tutorial, you'll learn how the Java environment works, the purpose of the JDK, JRE, and JVM, how Java programs are executed, how to configure environment variables, and the most commonly used Java commands.
Table of Contents
- What is the Java Environment?
- JDK (Java Development Kit)
- JRE (Java Runtime Environment)
- JVM (Java Virtual Machine)
- Bytecode
- Java Compiler (javac)
- Java Program Execution Process
- PATH Variable
- Environment Variables
- Java Commands
- Summary
- Practice Exercises
What is the Java Environment?
The Java Environment is the complete setup required to develop and run Java applications.
It includes:
- Java Development Kit (JDK)
- Java Runtime Environment (JRE)
- Java Virtual Machine (JVM)
- Java Compiler
- Java Standard Libraries
- Command-line tools
Together, these components allow developers to write, compile, and execute Java programs on different operating systems.
JDK (Java Development Kit)
The Java Development Kit (JDK) is a software package used to develop Java applications.
If you want to write Java code, you must install the JDK.
JDK Includes
- Java Compiler (
javac) - Java Runtime Environment (JRE)
- Java Virtual Machine (JVM)
- Java Debugger
- Documentation tools
- Development utilities
Diagram
1JDK 2├── JRE 3│ ├── JVM 4│ └── Java Libraries 5├── javac 6├── javadoc 7├── jar 8└── Development Tools
Why Do We Need the JDK?
The JDK allows you to:
- Write Java programs
- Compile source code
- Debug applications
- Package applications
- Build Java projects
Without the JDK, you cannot compile Java source files.
JRE (Java Runtime Environment)
The Java Runtime Environment (JRE) provides everything needed to run Java applications.
It includes:
- JVM
- Core Java libraries
- Supporting files
The JRE does not include the Java compiler (javac).
Purpose of the JRE
The JRE is designed only for running Java programs, not developing them.
JVM (Java Virtual Machine)
The Java Virtual Machine (JVM) is the engine that executes Java bytecode.
It acts as a bridge between Java bytecode and the operating system.
Responsibilities of the JVM
- Loads Java classes
- Verifies bytecode
- Executes bytecode
- Allocates memory
- Performs garbage collection
- Manages threads
- Handles runtime exceptions
Advantages of the JVM
- Platform independence
- Automatic memory management
- Security
- Performance optimizations using JIT compilation
Relationship Between JDK, JRE, and JVM
1JDK 2│ 3├── JRE 4│ │ 5│ └── JVM
Comparison Table
| Feature | JDK | JRE | JVM |
|---|---|---|---|
| Write Java Programs | ✅ | ❌ | ❌ |
| Compile Java Code | ✅ | ❌ | ❌ |
| Run Java Programs | ✅ | ✅ | ✅ |
| Contains Compiler | ✅ | ❌ | ❌ |
| Contains JVM | ✅ | ✅ | Self |
Easy Analogy
Imagine building and driving a car:
- JDK → The factory with all the tools to build the car.
- JRE → The completed car ready to drive.
- JVM → The engine that powers the car.
Bytecode
Java source code cannot be executed directly by the operating system.
Instead, it is converted into bytecode.
Source File
1Hello.java
After compilation:
1Hello.class
The .class file contains bytecode, which can run on any operating system with a compatible JVM.
Why Bytecode?
Bytecode makes Java platform independent because every operating system has its own JVM implementation that understands the same bytecode.
Java Compiler (javac)
The Java Compiler translates Java source code into bytecode.
Compilation Process
1Hello.java 2 │ 3 ▼ 4 javac Compiler 5 │ 6 ▼ 7Hello.class
Compile a Program
1javac Hello.java
If there are no syntax errors, a .class file is generated.
Java Program Execution Process
Understanding how Java executes a program is essential.
Step 1: Write the Source Code
1Hello.java
↓
Step 2: Compile
1javac Hello.java
↓
Step 3: Generate Bytecode
1Hello.class
↓
Step 4: JVM Loads the Class
The JVM loads the bytecode into memory.
↓
Step 5: Bytecode Verification
The JVM checks the bytecode for security and correctness.
↓
Step 6: Execution
The JVM interprets or JIT-compiles the bytecode into machine code and executes the program.
Complete Flow
1Source Code (.java) 2 │ 3 ▼ 4Java Compiler (javac) 5 │ 6 ▼ 7Bytecode (.class) 8 │ 9 ▼ 10Class Loader 11 │ 12 ▼ 13Bytecode Verifier 14 │ 15 ▼ 16JVM Execution Engine 17 │ 18 ▼ 19Operating System 20 │ 21 ▼ 22Output
PATH Variable
The PATH variable is an operating system environment variable that tells the system where to look for executable programs.
When you type:
1java
the operating system searches the directories listed in the PATH variable to find the java executable.
Without the correct PATH configuration, you may see errors such as:
1'java' is not recognized as an internal or external command.
or
1java: command not found
Example PATH (Windows)
1C:\Program Files\Java\jdk-21\bin
Example PATH (Linux/macOS)
1/usr/lib/jvm/jdk-21/bin
Environment Variables
Environment variables store configuration values that applications and the operating system use.
Common Java-related environment variables include:
PATH
Contains the location of Java executables.
JAVA_HOME
Points to the JDK installation directory.
Example (Windows):
1JAVA_HOME=C:\Program Files\Java\jdk-21
Example (Linux/macOS):
1JAVA_HOME=/usr/lib/jvm/jdk-21
Many development tools such as Maven, Gradle, and IDEs use JAVA_HOME to locate the JDK.
Java Commands
Here are some of the most commonly used Java commands.
Check Java Version
1java -version
Example output:
1openjdk version "21.0.x"
Check Java Compiler Version
1javac -version
Example output:
1javac 21.0.x
Compile a Java Program
1javac Hello.java
Creates:
1Hello.class
Run a Java Program
1java Hello
Output:
1Hello World
Do not include the
.classextension when running a Java program.
Display Java Help
1java --help
Displays available options for the java command.
Display Compiler Help
1javac --help
Shows available compiler options.
Common Java Commands Cheat Sheet
| Command | Description |
|---|---|
java -version | Display installed Java version |
javac -version | Display compiler version |
javac File.java | Compile a Java source file |
java ClassName | Run a compiled Java class |
java --help | Show Java command options |
javac --help | Show compiler options |
Summary
In this tutorial, you learned:
- What the Java environment is
- The purpose of the JDK, JRE, and JVM
- How Java source code is converted into bytecode
- The role of the Java compiler (
javac) - The complete Java program execution process
- Why the PATH variable and
JAVA_HOMEare important - Essential Java command-line tools and commands
Understanding these concepts gives you a strong foundation for developing and running Java applications and prepares you for the next topics, such as Java syntax, variables, data types, and object-oriented programming.
Practice Exercises
-
Install the latest JDK (Java 17 or Java 21 LTS) on your computer.
-
Verify the installation using:
java -versionjavac -version
-
Set the
JAVA_HOMEenvironment variable and add the JDK'sbindirectory to yourPATH. -
Create a file named
Hello.javathat prints"Welcome to Java!". -
Compile the program using
javac Hello.java. -
Run the compiled program using
java Hello. -
Explain the difference between the JDK, JRE, and JVM in your own words.
-
Draw the Java execution process from source code to program output using a flowchart.