Download and Install Java

What Do You Need to Install?

To write and run Java programs you need the Java Development Kit (JDK). It includes:

  • javac — the Java compiler that turns your source code into bytecode
  • java — the JVM launcher that runs bytecode
  • jar, javadoc — packaging and documentation tools
  • JRE — the runtime environment (bundled inside the JDK)

💡 Which version? Always prefer a Long Term Support (LTS) release for stability. The current LTS versions are Java 17 and Java 21. Java 21 is recommended for new projects.

LTS Version Overview

Version Type Release Date Support Until Recommendation
Java 8 LTS Mar 2014 Dec 2030 Legacy projects only
Java 11 LTS Sep 2018 Sep 2026 Still widely used
Java 17 LTS Sep 2021 Sep 2029 Stable choice
Java 21 LTS Sep 2023 Sep 2031 ✅ Recommended

Installation Guide

Install on Windows

1
Download the JDK installer

Go to Oracle JDK downloads or Adoptium (OpenJDK). Choose Windows x64 Installer (.msi) for Java 21.

2
Run the installer

Double-click the downloaded .msi file. Click Next through the wizard and accept the default install path:
C:\Program Files\Java\jdk-21

3
Set the JAVA_HOME environment variable
  1. Open Start ? search Environment Variables ? click Edit the system environment variables.
  2. Click Environment Variables button.
  3. Under System variables click New:
    • Variable name: JAVA_HOME
    • Variable value: C:\Program Files\Java\jdk-21
  4. Find the Path variable ? click Edit ? click New ? add: %JAVA_HOME%\bin
  5. Click OK on all dialogs.
4
Verify the installation

Open Command Prompt and run:

java -version
javac -version

Expected output:

java version "21.0.3" 2024-04-16 LTS javac 21.0.3

Install on macOS

Option A — Using Homebrew (Recommended)

1
Install Homebrew (if not already installed)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
2
Install Java 21
brew install openjdk@21
3
Link it and set JAVA_HOME
sudo ln -sfn $(brew --prefix openjdk@21)/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-21.jdk
echo 'export JAVA_HOME=$(brew --prefix openjdk@21)' >> ~/.zshrc
source ~/.zshrc

Option B — Manual .pkg Installer

1
Download the macOS .pkg from Oracle or Adoptium for Java 21.
2
Double-click the .pkg and follow the installer wizard.
3
Add JAVA_HOME to your shell profile:
echo 'export JAVA_HOME=$(/usr/libexec/java_home -v 21)' >> ~/.zshrc
source ~/.zshrc
4
Verify
java -version
javac -version
openjdk version "21.0.3" 2024-04-16 javac 21.0.3

Install on Linux

Ubuntu / Debian

1
Update packages and install OpenJDK 21
sudo apt update
sudo apt install openjdk-21-jdk -y
2
Set JAVA_HOME
echo 'export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64' >> ~/.bashrc
echo 'export PATH=$JAVA_HOME/bin:$PATH' >> ~/.bashrc
source ~/.bashrc

Fedora / RHEL / CentOS

1
sudo dnf install java-21-openjdk-devel -y

Arch Linux

1
sudo pacman -S jdk21-openjdk
2
Verify (all distros)
java -version
javac -version
openjdk version "21.0.3" 2024-04-16 javac 21.0.3

Verify Your Installation

Open a terminal (or Command Prompt on Windows) and run these two commands:

java -version
javac -version

Both commands must return a version number. If you get "command not found" or "not recognized", the PATH is not set correctly — revisit Step 3 of your OS section above.

⚠️ Common mistake: Installing JRE instead of JDK. The JRE does not include javac. Make sure you downloaded the JDK.

Write and Run Your First Program

Once Java is installed, confirm everything works end-to-end:

1
Create a file named HelloWorld.java with this content:
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Java is installed and working!");
    }
}
2
Compile it
javac HelloWorld.java

This creates a HelloWorld.class file in the same folder.

3
Run it
java HelloWorld
Java is installed and working!

You're all set! Java is correctly installed and your first program ran successfully.

Oracle JDK vs OpenJDK

Feature Oracle JDK OpenJDK (Adoptium / Amazon Corretto)
License Free for dev/testing; paid for production 100% free and open source
Performance Identical Identical
Long-term support Yes (Oracle subscription) Yes (community / vendor)
Recommended for Oracle-supported enterprise environments Most developers and projects

📌 For personal learning and most professional projects, OpenJDK from Adoptium (Eclipse Temurin) is the best choice — fully free with LTS support.

Managing Multiple Java Versions

If you need to switch between Java versions (e.g. Java 11 for an old project and Java 21 for a new one), use a version manager:

  • SDKMAN (macOS / Linux) — sdk install java 21-tem then sdk use java 21-tem
  • Jabba (Windows / macOS / Linux) — cross-platform JDK version manager
  • Windows: manually update JAVA_HOME to point to the desired version folder
# SDKMAN example (macOS/Linux)
sdk list java          # see available versions
sdk install java 21-tem
sdk install java 17-tem
sdk use java 17-tem    # switch for current session
sdk default java 21-tem # set global default

Continue learning