-
-
Notifications
You must be signed in to change notification settings - Fork 144
Java 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.
- macOS Installation
- Windows Installation
- Linux Installation
- Setting up JAVA_HOME
- Verifying Installation
-
Update Homebrew:
brew update
-
Install OpenJDK 21:
brew install openjdk@21
-
Create symlink (if needed):
sudo ln -sfn $(brew --prefix openjdk@21)/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-21.jdk
-
Download the macOS
.dmginstaller from: -
Run the installer and follow the prompts.
-
Download the installer:
- Visit Oracle JDK Downloads or Adoptium
- Download the Windows
.msior.exeinstaller for Java 21
-
Run the installer:
- Double-click the downloaded file
- Follow the installation wizard
- Note the installation path (typically
C:\Program Files\Java\jdk-21)
-
Continue to Setting up JAVA_HOME on Windows
- Download the ZIP version of JDK 21
- Extract to a directory (e.g.,
C:\Java\jdk-21) - Continue to Setting up JAVA_HOME on Windows
# Update package lists
sudo apt update
# Install OpenJDK 21
sudo apt install openjdk-21-jdk
# Verify installation
java -version# Install OpenJDK 21
sudo yum install java-21-openjdk-devel
# Or for newer versions:
sudo dnf install java-21-openjdk-devel# Install OpenJDK 21
sudo pacman -S jdk21-openjdk-
Download the tar.gz archive:
wget https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.tar.gz
-
Extract to /opt:
sudo mkdir -p /opt/java sudo tar -xzf jdk-21_linux-x64_bin.tar.gz -C /opt/java
-
Continue to Setting up JAVA_HOME on Linux
-
Find your Java installation path:
/usr/libexec/java_home -v 21
-
Add to your shell profile (
.zshrcfor Zsh or.bash_profilefor Bash):# Open your profile nano ~/.zshrc # Add these lines: export JAVA_HOME=$(/usr/libexec/java_home -v 21) export PATH="$JAVA_HOME/bin: $PATH"
-
Reload your profile:
source ~/.zshrc
-
Open Environment Variables:
- Right-click on "This PC" or "My Computer"
- Select "Properties"
- Click "Advanced system settings"
- Click "Environment Variables"
-
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"
-
Update PATH variable:
- Find "Path" under "System variables"
- Click "Edit"
- Click "New"
- Add:
%JAVA_HOME%\bin - Click "OK" on all dialogs
-
Restart Command Prompt or PowerShell for changes to take effect
-
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
-
Reload your profile:
source ~/.bashrc
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)
# 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# Install jEnv
brew install jenv
# Add Java version
jenv add $(/usr/libexec/java_home -v 21)
# Set global version
jenv global 21- Ensure
JAVA_HOME/binis in your PATH - Restart your terminal or IDE
- 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"
- Use version management tools (SDKMAN!, jEnv)
- Ensure your PATH prioritizes the correct Java installation
The JAVA_HOME environment variable is essential because:
-
Build Tools: Maven, Gradle, and Ant use
JAVA_HOMEto locate the JDK - IDEs: IntelliJ IDEA, Eclipse, and VS Code rely on it for project configuration
- Application Servers: Tomcat, WildFly, and other servers need it to run
-
Scripts: Many shell scripts and batch files reference
JAVA_HOME - Consistency: Provides a single source of truth for Java location across all tools