Installing Java (JDK 21)
Reading Time: 8 Minutes
Difficulty: Beginner
Topic Summaryโ
Before you write a single line of Java, you need to install the JDK (Java Development Kit) on your computer. This lesson walks you through installing JDK 21 (the latest LTS version) on Windows, setting the PATH environment variable, and choosing an editor โ VS Code or IntelliJ IDEA.
What You'll Learnโ
- How to download and install JDK 21 on Windows
- What the JAVA_HOME environment variable is
- How to verify Java is installed correctly with
java -version - How to set up VS Code with Java extensions
- How to set up IntelliJ IDEA Community Edition
Prerequisitesโ
- Lesson 004 โ JDK vs JRE vs JVM (understand what JDK is)
- A Windows computer with internet access
- Basic knowledge of navigating Windows (Control Panel, Environment Variables)
Explanationโ
Why JDK 21?โ
Java 21 is the latest LTS (Long-Term Support) version released in September 2023. LTS means:
- Security updates for years to come
- Stable, production-ready
- Supported by all major frameworks (Spring Boot, Quarkus, etc.)
Always use an LTS version for learning and projects.
Step 1: Download JDK 21โ
You have two options โ both are free:
Option A: Oracle JDK 21 (Recommended for beginners)โ
- Go to: https://www.oracle.com/java/technologies/downloads/
- Click on Java 21
- Under the Windows tab, download the x64 Installer (
.exefile) - File will be named something like:
jdk-21_windows-x64_bin.exe
Option B: OpenJDK 21 (100% open source)โ
- Go to: https://adoptium.net/ (Eclipse Temurin โ trusted OpenJDK build)
- Select: Temurin 21 (LTS)
- Choose: Windows x64 JDK
- Download the
.msiinstaller
Tip: For learning, both work identically. OpenJDK/Adoptium is what most developers use professionally.
Step 2: Install JDK 21โ
- Run the installer (double-click the
.exeor.msifile) - Click Next through the installation wizard
- Note the installation path โ usually:
C:\Program Files\Java\jdk-21orC:\Program Files\Eclipse Adoptium\jdk-21.x.x.x-hotspot - Click Install
- Click Finish when done
Step 3: Set JAVA_HOME Environment Variableโ
This tells Windows (and other tools like Maven, Gradle, IDEs) where Java is installed.
Method: Using Windows Settingsโ
- Press Windows key + S, search for "Environment Variables"
- Click "Edit the system environment variables"
- Click the "Environment Variables..." button
- Under "System Variables", click New:
- Variable name:
JAVA_HOME - Variable value:
C:\Program Files\Java\jdk-21(your actual install path)
- Variable name:
- Click OK
Add Java to PATHโ
- In the same Environment Variables window, find Path under System Variables
- Click Edit โ New
- Add:
%JAVA_HOME%\bin - Click OK โ OK โ OK to close all windows
Step 4: Verify Installationโ
Open a new Command Prompt or PowerShell window (important โ reopen it to load new environment variables):
# Check Java version
java -version
# Check compiler version
javac -version
# Check JAVA_HOME
echo %JAVA_HOME%
Expected Output:โ
java version "21.0.1" 2023-10-17 LTS
Java(TM) SE Runtime Environment (build 21.0.1+12-LTS-29)
Java HotSpot(TM) 64-Bit Server VM (build 21.0.1+12-LTS-29, mixed mode, sharing)
javac 21.0.1
C:\Program Files\Java\jdk-21
If you see the version numbers โ Java is installed! ๐
Step 5: Choose Your Code Editor/IDEโ
You have two great free options:
Option A: VS Code + Extension Pack for Java โญ (Recommended for beginners)โ
Why VS Code?
- Free and lightweight
- Works for all programming languages
- Great Java support with Microsoft's extension pack
Installation:
- Download VS Code: https://code.visualstudio.com/
- Install VS Code
- Open VS Code
- Press
Ctrl + Shift + Xto open Extensions - Search for: "Extension Pack for Java" (by Microsoft)
- Click Install
This installs 6 extensions:
- Language Support for Java (by Red Hat)
- Debugger for Java
- Test Runner for Java
- Maven for Java
- Project Manager for Java
- IntelliCode
Create and run your first Java file in VS Code:
- Create a folder:
C:\JavaProjects - Open VS Code โ File โ Open Folder โ select
C:\JavaProjects - Create file:
HelloWorld.java - Write your code
- Click the โถ Run button (top right) โ VS Code compiles and runs automatically!
Option B: IntelliJ IDEA Community Edition (Recommended when you're more comfortable)โ
Why IntelliJ?
- The most popular Java IDE in the industry
- Community Edition is completely FREE
- Smart code completion, refactoring, debugging
Installation:
- Go to: https://www.jetbrains.com/idea/download/
- Download Community edition (NOT Ultimate โ that's paid)
- Run the installer
- Open IntelliJ IDEA
- Click "New Project"
- Select Java on the left
- Under SDK, select your installed JDK 21
- Click Next โ Finish
Comparison: VS Code vs IntelliJโ
| Feature | VS Code | IntelliJ IDEA Community |
|---|---|---|
| Price | Free | Free |
| Weight | Lightweight (~80 MB) | Heavy (~600 MB) |
| Languages | All languages | Mainly Java/JVM |
| Java Support | Good (with extensions) | Excellent (built-in) |
| Beginner friendly | โ Yes | Moderate |
| Industry use | Very common | Most common for Java |
| Recommended for | Beginners | Intermediate+ |
Recommendation: Start with VS Code while learning the basics. Move to IntelliJ when you start building real projects.
Real-World Analogyโ
Installing Java is like setting up a workshop:
- JDK = Your complete tool set (hammer, screwdriver, drill)
- JAVA_HOME = Telling everyone where the toolbox is stored
- PATH = Like labeling the hallway so anyone can find the toolbox quickly
- VS Code / IntelliJ = Your workbench where you actually build things
Without knowing where the tools are (JAVA_HOME & PATH), nothing can use them!
Code Exampleโ
Once installed, open your terminal, navigate to a folder, and try this:
# In PowerShell or Command Prompt
# 1. Create a test file
# Open Notepad, write the code below, save as HelloWorld.java
# 2. Compile it
javac HelloWorld.java
# 3. Run it
java HelloWorld
// HelloWorld.java โ Your very first Java program!
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Java is installed and working!");
System.out.println("Java version: " + System.getProperty("java.version"));
}
}
Outputโ
Java is installed and working!
Java version: 21.0.1
Common Mistakesโ
- โ Mistake:
javacommand not found after installing โ โ Fix: Close and reopen the terminal. If still not working, check PATH has%JAVA_HOME%\bin. - โ Mistake: Multiple Java versions causing conflicts โ โ
Fix: Check
java -version. If wrong version shows, update JAVA_HOME and PATH to point to JDK 21. - โ Mistake: Downloading JRE instead of JDK โ โ Fix: Always download JDK (has javac compiler). JRE alone can't compile.
- โ Mistake: File saved as
HelloWorld.java.txtin Notepad โ โ Fix: In Notepad's "Save As" dialog, change "Save as type" to "All Files (.)" and name itHelloWorld.java.
Best Practicesโ
- Always install the latest LTS version of JDK (currently 21)
- Set JAVA_HOME properly โ many tools depend on it
- Use VS Code or IntelliJ โ avoid writing Java in Notepad for real projects
- If you work on multiple projects needing different Java versions, use SDKMAN (Linux/Mac) or jEnv to manage multiple JDK versions
Interview Questionsโ
Q: What is JAVA_HOME and why is it needed?
A: JAVA_HOME is an environment variable that points to the JDK installation directory. Many Java tools, IDEs, and build systems (Maven, Gradle) use JAVA_HOME to find the JDK they need to compile and run Java programs.
Q: What is the difference between the java and javac commands?
A: javac is the Java compiler โ it compiles .java source files into .class bytecode files. java is the Java launcher โ it starts the JVM and runs the compiled .class file.
Q: Why should I use JDK 21 instead of JDK 8?
A: JDK 21 is the latest LTS version with modern features (virtual threads, records, pattern matching), better performance, and active security support. JDK 8 is old (2014) and while still maintained, lacks modern improvements.
Quick Revisionโ
โ Download JDK 21 from oracle.com or adoptium.net
โ Set JAVA_HOME to JDK installation path
โ Add %JAVA_HOME%\bin to system PATH
โ Verify: java -version and javac -version
โ Use VS Code + Extension Pack for Java (beginners) or IntelliJ IDEA Community (intermediate)
Related Topicsโ
- JDK vs JRE vs JVM (Lesson 004)
- Java Program Execution (Lesson 005)
- Your First Java Program (Lesson 007)
Next Lessonโ
Lesson 007 โ Your First Java Program