How to set or change the default Java (JDK) version on macOS?

Learn how to set or change the default java (jdk) version on macos? with practical examples, diagrams, and best practices. Covers java, macos development techniques with visual explanations.

Mastering Java Versions on macOS: A Comprehensive Guide

Hero image for How to set or change the default Java (JDK) version on macOS?

Learn how to effectively manage, switch, and set the default Java Development Kit (JDK) version on your macOS system, ensuring compatibility and smooth development workflows.

Developing on macOS often involves working with multiple Java projects, each potentially requiring a different JDK version. This guide will walk you through the essential steps to install, verify, and switch between various Java versions, as well as how to set a default for your system. Understanding these techniques is crucial for maintaining a flexible and efficient development environment.

Understanding Java Installation on macOS

On macOS, Java installations are typically managed in a few key locations. Oracle JDKs and OpenJDK distributions often reside in /Library/Java/JavaVirtualMachines/. Homebrew, a popular package manager for macOS, installs JDKs in its cellar, usually under /usr/local/Cellar/openjdk/ or similar paths, and then symlinks them into /usr/local/opt/openjdk/ or /usr/local/openjdk/. Knowing these locations is fundamental to managing your Java environment.

ls -l /Library/Java/JavaVirtualMachines/
ls -l /usr/local/opt/openjdk/

Check common JDK installation directories

Verifying Your Current Java Version

Before making any changes, it's good practice to check which Java version is currently active on your system. This helps you understand your starting point and confirm changes after you've made them.

java -version
javac -version

Commands to check the currently active Java runtime and compiler versions.

flowchart TD
    A[Start] --> B{Check `java -version`};
    B --> C{Is it the desired version?};
    C -- Yes --> D[No action needed];
    C -- No --> E[Proceed to change version];
    E --> F[End];

Workflow for verifying the current Java version.

Installing New JDK Versions with Homebrew

Homebrew makes installing different JDK versions straightforward. You can install specific versions of OpenJDK or other distributions with simple commands. This is the preferred method for most developers.

# Install OpenJDK 17
brew install openjdk@17

# Install OpenJDK 11
brew install openjdk@11

# List all installed OpenJDK versions via Homebrew
brew list --formula | grep openjdk

Examples of installing and listing JDKs using Homebrew.

Setting the Default Java Version

There are several ways to set the default Java version, ranging from system-wide configurations to user-specific environment variables. The most common and flexible approach involves modifying your shell's configuration file (e.g., .zshrc or .bash_profile).

1. Step 1: Locate your shell configuration file

Open your terminal and determine which shell you are using. For Zsh, it's ~/.zshrc. For Bash, it's typically ~/.bash_profile or ~/.bashrc.

2. Step 2: Open the configuration file for editing

Use a text editor like nano or vim to open your shell's configuration file. For example: nano ~/.zshrc.

3. Step 3: Add or modify the JAVA_HOME environment variable

Add the following lines to the end of your configuration file, replacing /path/to/your/jdk with the actual path to your desired JDK installation. For Homebrew installations, this is usually /usr/local/opt/openjdk@<version>/libexec/openjdk.jdk/Contents/Home.

4. Step 4: Update your PATH environment variable

Ensure that the bin directory of your JAVA_HOME is at the beginning of your PATH. This tells your shell to look for Java executables in your chosen JDK first.

5. Step 5: Apply the changes

Save the file and then source your configuration file to apply the changes without restarting your terminal: source ~/.zshrc (or source ~/.bash_profile).

6. Step 6: Verify the new default version

Run java -version and javac -version again to confirm that your system is now using the newly configured JDK.

# Example for OpenJDK 17 installed via Homebrew
export JAVA_HOME="/usr/local/opt/openjdk@17/libexec/openjdk.jdk/Contents/Home"
export PATH="$JAVA_HOME/bin:$PATH"

# For older Homebrew installations, it might be:
# export JAVA_HOME="/usr/local/opt/openjdk/libexec/openjdk.jdk/Contents/Home"
# export PATH="$JAVA_HOME/bin:$PATH"

Adding JAVA_HOME and updating PATH in your shell configuration.

Using jenv for Advanced Java Version Management

For developers who frequently switch between many Java versions for different projects, jenv is an invaluable tool. It allows you to set global, local (per-project), and shell-specific Java versions with ease, similar to nvm for Node.js or rvm for Ruby.

1. Step 1: Install jenv

Install jenv using Homebrew: brew install jenv.

2. Step 2: Configure your shell

Add jenv to your shell's configuration file (.zshrc or .bash_profile). Follow the instructions provided by jenv init.

3. Step 3: Add your JDKs to jenv

Tell jenv about your installed JDKs. It can often auto-detect them, or you can add them manually: jenv add /Library/Java/JavaVirtualMachines/jdk-17.0.2.jdk/Contents/Home.

4. Step 4: Set global or local Java versions

Use jenv global 17.0 to set Java 17 as the default globally, or navigate to a project directory and use jenv local 11.0 to set Java 11 for that specific project.

5. Step 5: Verify

Run java -version and jenv versions to confirm the active Java version and list all managed versions.

# Install jenv
brew install jenv

# Initialize jenv in your shell (follow output instructions)
echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(jenv init -)"' >> ~/.zshrc
source ~/.zshrc

# Add a JDK (replace with your actual path)
jenv add /Library/Java/JavaVirtualMachines/jdk-17.0.2.jdk/Contents/Home

# List available JDKs in jenv
jenv versions

# Set a global default
jenv global 17.0.2

# Set a local default for a project
mkdir my-java-project && cd my-java-project
jenv local 11.0.13

Basic jenv commands for installation and version management.