Installing JDK 11 on macOS: A Comprehensive Guide
Java Development Kit (JDK) 11 is a long-term support (LTS) release, making it a popular choice for enterprise and production environments due to its stability, security updates, and extended support周期 (until September 2026 for Oracle JDK). Whether you’re a developer building Java applications, running legacy software, or setting up a development environment, installing JDK 11 on macOS requires careful attention to compatibility, installation methods, and configuration.
This guide will walk you through prerequisites, download options, step-by-step installation methods (GUI and command-line), verification, environment setup, best practices, and troubleshooting common issues. By the end, you’ll have a fully functional JDK 11 environment on your Mac.
Table of Contents#
- Prerequisites
- JDK 11 Download Options
- Installation Methods
- Verify JDK 11 Installation
- Set Up Environment Variables (JAVA_HOME)
- Best Practices
- Common Issues and Troubleshooting
- Example Usage: Test JDK 11
- References
Prerequisites#
Before installing JDK 11, ensure your macOS meets the following requirements:
- macOS Version: macOS 10.13 (High Sierra) or later. For Apple Silicon (M1/M2) Macs, JDK 11 requires Rosetta 2 (for x86_64 builds) or a native aarch64 build (e.g., Eclipse Temurin).
- Storage: At least 200 MB of free disk space.
- Admin Rights: You need administrator privileges to install software on macOS.
Check for Existing Java Installations:
To avoid conflicts, first check if Java is already installed. Open Terminal and run:
java -versionIf Java is installed, the output will show the version (e.g., openjdk version "1.8.0_302"). If not, you’ll see an error like No Java runtime present.
JDK 11 Download Options#
JDK 11 is available in two main flavors: Oracle JDK (proprietary, requires a license for commercial use) and OpenJDK (open-source, free for all use cases). We recommend OpenJDK for most users due to its permissive license.
Oracle JDK 11#
Oracle JDK 11 is free for development and testing but requires a commercial license for production use.
Steps to Download:
- Go to the Oracle JDK 11 Download Page.
- Accept the license agreement.
- Download the appropriate DMG file:
- For Intel Macs:
jdk-11.<version>_osx-x64_bin.dmg - For Apple Silicon (M1/M2): Oracle JDK 11 does not natively support aarch64; use Rosetta 2 with the x64 DMG or switch to OpenJDK.
- For Intel Macs:
OpenJDK Distributions (Recommended)#
OpenJDK is free, open-source, and maintained by communities/companies like Eclipse (Temurin), Amazon (Corretto), and Azul (Zulu). We’ll use Eclipse Temurin (formerly AdoptOpenJDK) for this guide, as it’s widely trusted and offers native aarch64 builds for Apple Silicon.
Steps to Download Eclipse Temurin 11:
- Go to the Eclipse Temurin 11 Download Page.
- Select:
- Version: 11 (LTS)
- Operating System: macOS
- Architecture:
x64(Intel) oraarch64(Apple Silicon) - Package Type:
.dmg
- Click “Download” to save the DMG file.
Installation Methods#
Method 1: Install via DMG (GUI)#
This method uses the graphical installer and is ideal for users who prefer a point-and-click interface.
Steps:#
- Locate the DMG File: Find the downloaded
temurin-11.<version>_macos-<arch>_bin.dmg(or Oracle JDK DMG) in yourDownloadsfolder. - Mount the DMG: Double-click the DMG file to mount it. A new window will open with a
.pkginstaller (e.g.,Temurin-11.pkg). - Run the Installer: Double-click the
.pkgfile to launch the installer. - Follow the Wizard:
- Click “Continue” on the welcome screen.
- Read the license agreement and click “Agree.”
- Select the destination (usually “Macintosh HD”) and click “Install.”
- Enter your macOS admin password when prompted.
- Complete Installation: Click “Close” when the installer finishes.
Default Installation Path:
JDK 11 is installed to:
/Library/Java/JavaVirtualMachines/temurin-11.jdk/Contents/Home/ # Temurin
# OR
/Library/Java/JavaVirtualMachines/jdk-11.<version>.jdk/Contents/Home/ # Oracle
Method 2: Install via Homebrew (Command-Line)#
Homebrew is a popular package manager for macOS. It simplifies JDK installation and updates.
Prerequisites: Install Homebrew#
If Homebrew isn’t installed, run this in Terminal:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"Verify installation with:
brew --version # Should output "Homebrew <version>"Install JDK 11 via Homebrew#
Homebrew uses “casks” for GUI applications, including JDKs. For Eclipse Temurin 11:
-
Update Homebrew:
brew update -
Install Temurin 11:
brew install --cask temurin11- This installs the latest Temurin 11 LTS version.
- For Intel Macs: This installs the x64 build.
- For Apple Silicon: This installs the aarch64 build (no Rosetta required).
-
Verify Installation Path:
Homebrew symlinks the JDK to:/Library/Java/JavaVirtualMachines/temurin-11.jdk/Contents/Home/
Verify JDK 11 Installation#
After installation, confirm JDK 11 is working correctly:
Step 1: Check Java Version#
Open Terminal and run:
java -versionExpected Output (Temurin example):
openjdk version "11.0.20" 2023-07-18
OpenJDK Runtime Environment Temurin-11.0.20+8 (build 11.0.20+8)
OpenJDK 64-Bit Server VM Temurin-11.0.20+8 (build 11.0.20+8, mixed mode)
Step 2: Check Compiler Version#
Verify the javac (Java compiler) is available:
javac -versionExpected Output:
javac 11.0.20
Step 3: Locate Installation Path#
To find where JDK 11 is installed, use:
/usr/libexec/java_home -v 11Example Output:
/Library/Java/JavaVirtualMachines/temurin-11.jdk/Contents/Home
Set Up Environment Variables (JAVA_HOME)#
Many tools (e.g., Maven, Gradle, IDEs) require the JAVA_HOME environment variable to locate the JDK. Here’s how to set it permanently:
Step 1: Determine Your Shell#
macOS uses zsh as the default shell (since Catalina). To confirm, run:
echo $SHELL # Output: /bin/zsh (or /bin/bash for older macOS)Step 2: Edit Shell Configuration File#
- For
zsh: Edit~/.zshrc - For
bash: Edit~/.bash_profileor~/.bashrc
Open the file in a text editor (e.g., nano):
nano ~/.zshrc # For zsh
# OR
nano ~/.bash_profile # For bashStep 3: Add JAVA_HOME#
Paste the following line (replace the path with your JDK location from java_home -v 11):
export JAVA_HOME=$(/usr/libexec/java_home -v 11)Step 4: Save and Apply Changes#
- Press
Ctrl+Oto save, thenCtrl+Xto exitnano. - Reload the shell config:
source ~/.zshrc # For zsh # OR source ~/.bash_profile # For bash
Step 5: Verify JAVA_HOME#
Run:
echo $JAVA_HOMEExpected Output:
/Library/Java/JavaVirtualMachines/temurin-11.jdk/Contents/Home
Best Practices#
- Use OpenJDK for Commercial Projects: Avoid Oracle JDK unless you have a commercial license. OpenJDK distributions like Temurin are free and fully compliant.
- Manage Multiple JDK Versions: Use tools like
jenvorsdkman!to switch between JDK versions (e.g., JDK 8, 11, 17).- Example with
jenv:brew install jenv jenv add /Library/Java/JavaVirtualMachines/temurin-11.jdk/Contents/Home jenv global 11.0.20 # Set JDK 11 as default
- Example with
- Keep JDK Updated: Regularly update JDK 11 to get security patches. For Homebrew:
brew upgrade --cask temurin - Avoid System-Wide JAVA_HOME: Set
JAVA_HOMEin user-specific shell config (e.g.,~/.zshrc) instead of system-wide files like/etc/profile. - Clean Up Old JDKs: Remove unused JDKs to save space:
sudo rm -rf /Library/Java/JavaVirtualMachines/old-jdk-folder/
Common Issues and Troubleshooting#
Issue 1: java: command not found#
Cause: JDK bin directory not in PATH.
Fix:
JAVA_HOMEis set, butPATHmay not include$JAVA_HOME/bin. Add this to your shell config:export PATH=$JAVA_HOME/bin:$PATH- Reload the config with
source ~/.zshrc(or equivalent).
Issue 2: java -version Shows an Older JDK#
Cause: An older JDK is prioritized in PATH.
Fix:
- Check
PATHwithecho $PATHand ensure$JAVA_HOME/bincomes before other Java paths. - Use
jenvto manage versions (see Best Practices).
Issue 3: Installation Fails on Apple Silicon (M1/M2)#
Cause: Using an x86_64 JDK without Rosetta 2.
Fix:
- Install Rosetta 2:
softwareupdate --install-rosetta - Or use a native aarch64 build (e.g., Temurin 11 aarch64 via Homebrew).
Issue 4: JAVA_HOME Not Persisting After Reboot#
Cause: Shell config file not loaded on startup.
Fix:
- For
zsh, ensure~/.zshrcis in your home directory. macOS loads~/.zshrcby default. - For
bash, use~/.bash_profile(loaded on login) instead of~/.bashrc(loaded for non-login shells).
Example Usage: Test JDK 11#
Let’s write a simple Java program to verify JDK 11 works:
Step 1: Create a Java File#
Open Terminal and run:
nano HelloWorld.javaPaste this code:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, JDK 11!");
// Test JDK 11 feature: Local variable type inference (var)
var message = "JDK 11 is working!";
System.out.println(message);
}
}Save and exit (Ctrl+O, Ctrl+X).
Step 2: Compile and Run#
Compile with javac:
javac HelloWorld.javaRun the class:
java HelloWorldExpected Output:
Hello, JDK 11!
JDK 11 is working!
References#
- Oracle JDK 11 Download
- Eclipse Temurin (OpenJDK 11)
- Homebrew Documentation
- jenv: JDK Version Manager
- Java SE 11 Documentation
- macOS Shell Configuration Guide
This guide covers everything you need to install, configure, and troubleshoot JDK 11 on macOS. By following these steps, you’ll have a robust Java development environment ready for building and running applications. Let us know in the comments if you encounter any issues!