Skip to main content

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:

  1. Go to: https://www.oracle.com/java/technologies/downloads/
  2. Click on Java 21
  3. Under the Windows tab, download the x64 Installer (.exe file)
  4. File will be named something like: jdk-21_windows-x64_bin.exe

Option B: OpenJDK 21 (100% open source)โ€‹

  1. Go to: https://adoptium.net/ (Eclipse Temurin โ€” trusted OpenJDK build)
  2. Select: Temurin 21 (LTS)
  3. Choose: Windows x64 JDK
  4. Download the .msi installer

Tip: For learning, both work identically. OpenJDK/Adoptium is what most developers use professionally.


Step 2: Install JDK 21โ€‹

  1. Run the installer (double-click the .exe or .msi file)
  2. Click Next through the installation wizard
  3. Note the installation path โ€” usually: C:\Program Files\Java\jdk-21 or C:\Program Files\Eclipse Adoptium\jdk-21.x.x.x-hotspot
  4. Click Install
  5. 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โ€‹

  1. Press Windows key + S, search for "Environment Variables"
  2. Click "Edit the system environment variables"
  3. Click the "Environment Variables..." button
  4. Under "System Variables", click New:
    • Variable name: JAVA_HOME
    • Variable value: C:\Program Files\Java\jdk-21 (your actual install path)
  5. Click OK

Add Java to PATHโ€‹

  1. In the same Environment Variables window, find Path under System Variables
  2. Click Edit โ†’ New
  3. Add: %JAVA_HOME%\bin
  4. 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:


Why VS Code?

  • Free and lightweight
  • Works for all programming languages
  • Great Java support with Microsoft's extension pack

Installation:

  1. Download VS Code: https://code.visualstudio.com/
  2. Install VS Code
  3. Open VS Code
  4. Press Ctrl + Shift + X to open Extensions
  5. Search for: "Extension Pack for Java" (by Microsoft)
  6. 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:

  1. Create a folder: C:\JavaProjects
  2. Open VS Code โ†’ File โ†’ Open Folder โ†’ select C:\JavaProjects
  3. Create file: HelloWorld.java
  4. Write your code
  5. Click the โ–ถ Run button (top right) โ€” VS Code compiles and runs automatically!

Why IntelliJ?

  • The most popular Java IDE in the industry
  • Community Edition is completely FREE
  • Smart code completion, refactoring, debugging

Installation:

  1. Go to: https://www.jetbrains.com/idea/download/
  2. Download Community edition (NOT Ultimate โ€” that's paid)
  3. Run the installer
  4. Open IntelliJ IDEA
  5. Click "New Project"
  6. Select Java on the left
  7. Under SDK, select your installed JDK 21
  8. Click Next โ†’ Finish

Comparison: VS Code vs IntelliJโ€‹

FeatureVS CodeIntelliJ IDEA Community
PriceFreeFree
WeightLightweight (~80 MB)Heavy (~600 MB)
LanguagesAll languagesMainly Java/JVM
Java SupportGood (with extensions)Excellent (built-in)
Beginner friendlyโœ… YesModerate
Industry useVery commonMost common for Java
Recommended forBeginnersIntermediate+

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: java command 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.txt in Notepad โ†’ โœ… Fix: In Notepad's "Save As" dialog, change "Save as type" to "All Files (.)" and name it HelloWorld.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)


  • 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