Installing Java on Windows — OpenJDK 21 Setup Guide
In this chapter, you’ll install the Java Development Kit (JDK) 21 on Windows. We’ll cover two methods — the graphical installer and the command-line approach — and verify everything works.
Method 1: Install Using Adoptium (Recommended)
Eclipse Adoptium provides free, production-ready OpenJDK builds called Eclipse Temurin.
Step 1: Download the Installer
- Go to https://adoptium.net/
- The site auto-detects your OS — click “Latest LTS Release” (Java 21)
- Download the
.msiinstaller for Windows x64
Step 2: Run the Installer
- Double-click the downloaded
.msifile - Click Next through the wizard
- Important: Check these options when they appear:
- ✅ Set JAVA_HOME variable
- ✅ Add to PATH
- Click Install and wait for completion
Step 3: Verify Installation
Open PowerShell (press Win + X, select “Windows PowerShell”) and run:
$ java -version
Expected output:
openjdk version "21.0.2" 2024-01-16 LTS
OpenJDK Runtime Environment Temurin-21.0.2+13 (build 21.0.2+13-LTS)
OpenJDK 64-Bit Server VM Temurin-21.0.2+13 (build 21.0.2+13-LTS, mixed mode, sharing)
$ javac -version
Expected output:
javac 21.0.2
Method 2: Install Using winget (Command Line)
If you prefer the command line, winget (Windows Package Manager) makes installation easy:
$ winget install Microsoft.OpenJDK.21
Or for Eclipse Temurin:
$ winget install EclipseAdoptium.Temurin.21.JDK
After installation, close and reopen PowerShell, then verify:
$ java -version
$ javac -version
Setting JAVA_HOME Manually
If the installer didn’t set JAVA_HOME automatically:
Step 1: Find the JDK Installation Path
The default location is usually:
C:\Program Files\Eclipse Adoptium\jdk-21.0.2+13
Or for Microsoft OpenJDK:
C:\Program Files\Microsoft\jdk-21.0.2.13-hotspot
Step 2: Set JAVA_HOME in Environment Variables
- Press
Win + Sand search for “Environment Variables” - Click “Edit the system environment variables”
- Click “Environment Variables…“ button
- Under System variables, click New:
- Variable name:
JAVA_HOME - Variable value:
C:\Program Files\Eclipse Adoptium\jdk-21.0.2+13(your actual path)
- Variable name:
- Find the Path variable under System variables, click Edit
- Click New and add:
%JAVA_HOME%\bin - Click OK on all dialogs
Step 3: Verify
Close and reopen PowerShell:
$ echo $env:JAVA_HOME
C:\Program Files\Eclipse Adoptium\jdk-21.0.2+13
$ java -version
openjdk version "21.0.2"
Test with Hello World
Create a folder for your Java projects and test:
$ mkdir $HOME\java-projects
$ cd $HOME\java-projects
Create a file called HelloWorld.java (use Notepad or any text editor):
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World! Java is working on Windows!");
}
}
Compile and run:
$ javac HelloWorld.java
$ java HelloWorld
Expected output:
Hello, World! Java is working on Windows!
Common Issues and Fixes
| Problem | Solution |
|---|---|
'java' is not recognized |
Close and reopen PowerShell. If still failing, add JDK bin folder to PATH manually |
'javac' is not recognized |
You have JRE, not JDK. Reinstall with JDK |
| Wrong version showing | Check PATH order — the first java.exe found wins. Remove old Java from PATH |
| UAC blocks installer | Right-click installer → “Run as administrator” |
Summary
| Step | Action |
|---|---|
| Download | adoptium.net → Java 21 LTS → Windows .msi |
| Install | Run installer, check “Set JAVA_HOME” and “Add to PATH” |
| Verify | java -version and javac -version in PowerShell |
| Test | Compile and run HelloWorld.java |