Installing Java
Before writing a line of Java you need a JDK (Java Development Kit) installed. This guide walks through installing a current LTS release on every major operating system, verifying it, and wiring up the environment variables your tools rely on.
Choosing a Distribution
OpenJDK is the open-source reference implementation, and several vendors package it into ready-to-install builds. Any of these are excellent, production-grade choices:
- Eclipse Temurin (by the Adoptium project) — the most popular vendor-neutral build.
- Oracle OpenJDK — Oracle’s free OpenJDK builds.
- Amazon Corretto, Azul Zulu, Microsoft Build of OpenJDK — other reputable distributions.
Tip: Pick a Long-Term Support (LTS) version such as 17, 21, or 25 for real projects. This guide assumes Java 21 (LTS), but the steps apply to any version.
macOS (Homebrew)
The simplest route on macOS is Homebrew.
# Install Temurin 21
brew install --cask temurin@21
# Verify
java -version
Homebrew casks place the JDK where macOS’s /usr/libexec/java_home utility can find it. Set JAVA_HOME in your shell profile (~/.zshrc on modern macOS):
echo 'export JAVA_HOME=$(/usr/libexec/java_home -v 21)' >> ~/.zshrc
echo 'export PATH="$JAVA_HOME/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc
Windows
- Download the Temurin 21 (MSI) installer from adoptium.net.
- Run the installer. On the Custom Setup screen, enable “Set JAVA_HOME variable” and “Add to PATH” — this configures everything for you.
- Open a new Command Prompt or PowerShell window and verify:
java -version
To configure JAVA_HOME manually (if you skipped the installer option), search for “Edit the system environment variables”, then add a system variable:
Variable name: JAVA_HOME
Variable value: C:\Program Files\Eclipse Adoptium\jdk-21
Then append %JAVA_HOME%\bin to the Path variable.
Linux
Option A — Package Manager (apt, Debian/Ubuntu)
sudo apt update
sudo apt install temurin-21-jdk # or: openjdk-21-jdk
java -version
Set JAVA_HOME in your shell profile:
echo 'export JAVA_HOME=$(dirname $(dirname $(readlink -f $(which java))))' >> ~/.bashrc
echo 'export PATH="$JAVA_HOME/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
Option B — SDKMAN! (any Linux/macOS)
SDKMAN! is the easiest way to install and switch between multiple JDK versions — ideal if you juggle several projects.
# Install SDKMAN!
curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
# List and install a JDK
sdk list java
sdk install java 21.0.4-tem # Temurin 21
# Switch the default version anytime
sdk default java 21.0.4-tem
Note: SDKMAN! manages
JAVA_HOMEandPATHfor you automatically whenever you switch versions — no manual profile edits needed.
Verifying the Installation
Two commands confirm a healthy install. java -version checks the runtime; javac -version confirms the compiler is present (proving you have a JDK, not just a runtime).
java -version
javac -version
Output:
openjdk version "21.0.4" 2024-07-16 LTS
OpenJDK Runtime Environment Temurin-21.0.4+7 (build 21.0.4+7-LTS)
OpenJDK 64-Bit Server VM Temurin-21.0.4+7 (build 21.0.4+7-LTS, mixed mode)
javac 21.0.4
Confirm JAVA_HOME points to the JDK:
# macOS / Linux
echo $JAVA_HOME
# Windows (PowerShell)
echo $env:JAVA_HOME
Warning: If
java -versionreports an old version after installing, you likely have an earlier JDK earlier in yourPATH. Ensure$JAVA_HOME/bincomes first, and always open a fresh terminal after changing environment variables.
Best Practices
- Always install an LTS release for production work.
- Set
JAVA_HOME— build tools like Maven and Gradle depend on it. - Use SDKMAN! (or Windows tooling like
jabba/scoop) when you need multiple JDK versions side by side. - Re-verify with
java -versionandjavac -versionafter every install or upgrade.