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#

  1. Prerequisites
  2. JDK 11 Download Options
  3. Installation Methods
  4. Verify JDK 11 Installation
  5. Set Up Environment Variables (JAVA_HOME)
  6. Best Practices
  7. Common Issues and Troubleshooting
  8. Example Usage: Test JDK 11
  9. 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 -version

If 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:

  1. Go to the Oracle JDK 11 Download Page.
  2. Accept the license agreement.
  3. 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.

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:

  1. Go to the Eclipse Temurin 11 Download Page.
  2. Select:
    • Version: 11 (LTS)
    • Operating System: macOS
    • Architecture: x64 (Intel) or aarch64 (Apple Silicon)
    • Package Type: .dmg
  3. 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:#

  1. Locate the DMG File: Find the downloaded temurin-11.<version>_macos-<arch>_bin.dmg (or Oracle JDK DMG) in your Downloads folder.
  2. Mount the DMG: Double-click the DMG file to mount it. A new window will open with a .pkg installer (e.g., Temurin-11.pkg).
  3. Run the Installer: Double-click the .pkg file to launch the installer.
  4. 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.
  5. 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:

  1. Update Homebrew:

    brew update
  2. 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).
  3. 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 -version

Expected 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 -version

Expected Output:

javac 11.0.20

Step 3: Locate Installation Path#

To find where JDK 11 is installed, use:

/usr/libexec/java_home -v 11

Example 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_profile or ~/.bashrc

Open the file in a text editor (e.g., nano):

nano ~/.zshrc  # For zsh
# OR
nano ~/.bash_profile  # For bash

Step 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+O to save, then Ctrl+X to exit nano.
  • Reload the shell config:
    source ~/.zshrc  # For zsh
    # OR
    source ~/.bash_profile  # For bash

Step 5: Verify JAVA_HOME#

Run:

echo $JAVA_HOME

Expected Output:

/Library/Java/JavaVirtualMachines/temurin-11.jdk/Contents/Home

Best Practices#

  1. Use OpenJDK for Commercial Projects: Avoid Oracle JDK unless you have a commercial license. OpenJDK distributions like Temurin are free and fully compliant.
  2. Manage Multiple JDK Versions: Use tools like jenv or sdkman! 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
  3. Keep JDK Updated: Regularly update JDK 11 to get security patches. For Homebrew:
    brew upgrade --cask temurin
  4. Avoid System-Wide JAVA_HOME: Set JAVA_HOME in user-specific shell config (e.g., ~/.zshrc) instead of system-wide files like /etc/profile.
  5. 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_HOME is set, but PATH may 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 PATH with echo $PATH and ensure $JAVA_HOME/bin comes before other Java paths.
  • Use jenv to 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 ~/.zshrc is in your home directory. macOS loads ~/.zshrc by 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.java

Paste 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.java

Run the class:

java HelloWorld

Expected Output:

Hello, JDK 11!
JDK 11 is working!

References#


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!