Skip to content

Java Installation Guide

Julien Von Der Marck edited this page Jan 13, 2026 · 1 revision

Java 21+ Installation Guide

This guide covers how to install Java 21 (or greater) on macOS, Windows, and Linux, along with setting up the JAVA_HOME environment variable.

Table of Contents


macOS Installation

Using Homebrew (Recommended)

  1. Update Homebrew:

    brew update
  2. Install OpenJDK 21:

    brew install openjdk@21
  3. Create symlink (if needed):

    sudo ln -sfn $(brew --prefix openjdk@21)/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-21.jdk

Alternative: Manual Installation

  1. Download the macOS .dmg installer from:

  2. Run the installer and follow the prompts.


Windows Installation

Using the Installer (Recommended)

  1. Download the installer:

  2. Run the installer:

    • Double-click the downloaded file
    • Follow the installation wizard
    • Note the installation path (typically C:\Program Files\Java\jdk-21)
  3. Continue to Setting up JAVA_HOME on Windows

Using ZIP Archive

  1. Download the ZIP version of JDK 21
  2. Extract to a directory (e.g., C:\Java\jdk-21)
  3. Continue to Setting up JAVA_HOME on Windows

Linux Installation

Debian/Ubuntu

# Update package lists
sudo apt update

# Install OpenJDK 21
sudo apt install openjdk-21-jdk

# Verify installation
java -version

Red Hat/CentOS/Fedora

# Install OpenJDK 21
sudo yum install java-21-openjdk-devel

# Or for newer versions:
sudo dnf install java-21-openjdk-devel

Arch Linux

# Install OpenJDK 21
sudo pacman -S jdk21-openjdk

Manual Installation (All Linux Distributions)

  1. Download the tar.gz archive:

    wget https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.tar.gz
  2. Extract to /opt:

    sudo mkdir -p /opt/java
    sudo tar -xzf jdk-21_linux-x64_bin.tar.gz -C /opt/java
  3. Continue to Setting up JAVA_HOME on Linux


Setting up JAVA_HOME

macOS

  1. Find your Java installation path:

    /usr/libexec/java_home -v 21
  2. Add to your shell profile (.zshrc for Zsh or .bash_profile for Bash):

    # Open your profile
    nano ~/.zshrc
    
    # Add these lines:
    export JAVA_HOME=$(/usr/libexec/java_home -v 21)
    export PATH="$JAVA_HOME/bin: $PATH"
  3. Reload your profile:

    source ~/.zshrc

Windows

  1. Open Environment Variables:

    • Right-click on "This PC" or "My Computer"
    • Select "Properties"
    • Click "Advanced system settings"
    • Click "Environment Variables"
  2. Create JAVA_HOME variable:

    • Under "System variables", click "New"
    • Variable name: JAVA_HOME
    • Variable value: C:\Program Files\Java\jdk-21 (adjust to your installation path)
    • Click "OK"
  3. Update PATH variable:

    • Find "Path" under "System variables"
    • Click "Edit"
    • Click "New"
    • Add: %JAVA_HOME%\bin
    • Click "OK" on all dialogs
  4. Restart Command Prompt or PowerShell for changes to take effect

Linux

  1. Add to your shell profile (.bashrc, .bash_profile, or .zshrc):

    # Open your profile
    nano ~/. bashrc
    
    # Add these lines (adjust path if using manual installation):
    export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64
    export PATH=$JAVA_HOME/bin: $PATH

    For manual installation in /opt:

    export JAVA_HOME=/opt/java/jdk-21
    export PATH=$JAVA_HOME/bin:$PATH
  2. Reload your profile:

    source ~/.bashrc

Verifying Installation

After installation and setting up JAVA_HOME, verify everything is working:

# Check Java version
java -version

# Check Java compiler version
javac -version

# Verify JAVA_HOME is set
echo $JAVA_HOME    # macOS/Linux
echo %JAVA_HOME%   # Windows (CMD)
echo $env:JAVA_HOME # Windows (PowerShell)

Expected output:

openjdk version "21.0.x" 2024-xx-xx
OpenJDK Runtime Environment (build 21.0.x+xx)
OpenJDK 64-Bit Server VM (build 21.0.x+xx, mixed mode, sharing)

Managing Multiple Java Versions

Using SDKMAN! (macOS/Linux)

# Install SDKMAN!
curl -s "https://get.sdkman.io" | bash

# Install Java 21
sdk install java 21-open

# Switch between versions
sdk use java 21-open

Using jEnv (macOS/Linux)

# Install jEnv
brew install jenv

# Add Java version
jenv add $(/usr/libexec/java_home -v 21)

# Set global version
jenv global 21

Troubleshooting

"java: command not found"

  • Ensure JAVA_HOME/bin is in your PATH
  • Restart your terminal or IDE

JAVA_HOME not recognized

  • Check for typos in environment variable names
  • Ensure you've restarted your terminal/command prompt
  • On Windows, verify you edited "System variables" not "User variables"

Multiple Java versions conflict

  • Use version management tools (SDKMAN!, jEnv)
  • Ensure your PATH prioritizes the correct Java installation

Why Set JAVA_HOME?

The JAVA_HOME environment variable is essential because:

  1. Build Tools: Maven, Gradle, and Ant use JAVA_HOME to locate the JDK
  2. IDEs: IntelliJ IDEA, Eclipse, and VS Code rely on it for project configuration
  3. Application Servers: Tomcat, WildFly, and other servers need it to run
  4. Scripts: Many shell scripts and batch files reference JAVA_HOME
  5. Consistency: Provides a single source of truth for Java location across all tools

Additional Resources