Home

JDK Environment Variable Configuration Process on macOS

Li

Li Wei

December 26, 20255 min read

Title: Configuring JDK Environment Variables on macOS

1. Problem Description

In macOS development you may encounter the following situations:

  • Running java -version in the terminal shows a high‑version JDK (e.g., JDK 17), but the project requires JDK 11.
  • Maven/Gradle builds fail with UnsupportedClassVersionError (bytecode version incompatibility).
  • The IDE (e.g., IntelliJ IDEA) reports “JDK version mismatch” or shows compilation errors.

Core issue: The system default JDK or the JDK set via environment variables does not match the version required by the project, leading to compatibility problems.

2. Causes

Common reasons for JDK version mismatches on macOS:

  • System default JDK is a newer version: macOS may ship with or automatically update to a newer JDK (e.g., macOS Ventura defaults to JDK 17).
  • Environment variables not explicitly set: Without specifying the target JDK path via JAVA_HOME, tools (terminal, Maven, etc.) fall back to the system’s newer JDK.
  • Multiple JDKs installed side by side: Tools like Homebrew or SDKMAN may have installed several JDKs (e.g., 8/11/17) but no explicit switch has been made.
  • IDE not configured: IDEs (such as IntelliJ) may rely on system environment variables or manual settings and thus fail to associate the correct JDK.

3. Solution

The steps below are tailored for macOS and cover everything from verifying installation to establishing a stable, long‑term setup.

Step 1: Verify that the target JDK (e.g., JDK 11) is installed

If JDK 11 is not yet installed, install it first (macOS users are encouraged to use Homebrew to install Zulu 11, which is open‑source and highly compatible).

Check installation:

  • Expected output (when JDK 11 is present):

    (If no JDK 11 path appears, it means the JDK is not installed.)

Install JDK 11 (if missing):

(installation commands go here)

Step 2: Temporary configuration (effective for the current terminal session)

Set JAVA_HOME and PATH dynamically via command line; this only affects the current terminal and is useful for quick testing.

Commands:

(command snippets)

Verify temporary effect:

(verification commands)

Step 3: Permanent configuration (effective for all terminal sessions)

Write the environment variables to macOS’s shell configuration file (Zsh by default) so they persist after restarting the terminal or opening a new window.

Procedure:

  • Confirm the current shell (macOS uses Zsh by default):

  • Edit the Zsh configuration file (recommended):

  • Add permanent configuration code:

    Append the following to the end of the file (the path to JDK 11 will be resolved automatically):

    (Optional: to use a fixed path, manually specify the JDK 11 location, e.g.:
    export JAVA_HOME=/Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home
    )*

  • Apply the changes:

    (source command or restart terminal)

Step 4: Multi‑version JDK management (recommended for flexible switching)

If you need to switch JDK versions frequently (e.g., 8/11/17), use the jenv tool (similar to how nvm manages Node.js) to simplify multi‑version handling.

Steps:

  • Install **jenv** via Homebrew:

  • Configure **jenv** in Zsh:

  • Register installed JDKs with **jenv**:

  • Switch JDK version:

    (commands to change the active JDK)

Step 5: Align build tools and IDE

Make sure Maven/Gradle and the IDE (e.g., IntelliJ) use the target JDK version, avoiding errors caused by reliance on system environment variables.

Maven configuration (explicitly specify the version is recommended):

Add maven-compiler-plugin to the project’s pom.xml to force the compiler to use version 11.

IDE configuration (IntelliJ IDEA example):

  • Open the project, then go to File → Project Structure (or Cmd + ;).
  • In the left‑hand menu select SDKs and click + → Add JDK.
  • Manually choose the JDK 11 path (e.g., /Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home).
  • Confirm the Project SDK selection is JDK 11 and check that the Language Level for each module in Modules is set to 11.

4. Verification

After completing the configuration, verify success with the following checks:

  • Terminal command verification:

  • Maven build verification:

  • IDE run verification:

  • Open any Java file; the status bar at the bottom right should display JDK 11.

  • Run the main method; no UnsupportedClassVersionError version‑related errors should appear.

  • Multi‑version switch verification (if using **jenv**):

    (commands to list and switch versions)

5. FAQ & Troubleshooting

Problem Possible Cause Solution
java -version still shows a higher version Environment variable not applied or JAVA_HOME mis‑configured Check that .zshrc is correct; re‑run source ~/.zshrc to apply
Maven compilation error invalid target release: 11 pom.xml not set Explicitly set source/target in maven-compiler-plugin to 11 for source and target
Version switch ineffective after jenv JDK not registered with jenv Re‑execute jenv add and restart the terminal
IDE shows “No JDK found” IDE cannot locate manually added JDK Manually add the JDK path in the IDE (see Step 5)
Terminal command java does nothing JDK directory permission issue Verify permissions of the JDK folder: chmod -R 755 /Library/Java/JavaVirtualMachines/zulu-11.jdk

Summary: By following the steps above you can quickly configure a specific JDK version (e.g., JDK 11) on macOS, resolve mismatches across the terminal, build tools, and IDE, and ensure a stable, efficient development workflow.


Originally written by Li Wei (李唯_) and published in Chinese on 后端技术栈全书 (Full-Stack Backend Engineering). Translated and adapted for DriftSeas with permission.

Keep reading

More related articles from DriftSeas.