How to Manage Multiple JDK Versions on macOS with Aliases and JEnv
This guide shows how to install several JDK versions on macOS, configure environment variables and aliases for manual switching, and use the jenv tool for precise, per‑directory Java version management, including installation, configuration, and common troubleshooting steps.
Introduction
When maintaining legacy Java projects you may need JDK 6, 7, or older, while new development requires the latest JDK. Installing multiple JDKs on macOS and switching between them efficiently solves this problem.
Switching JDK via Aliases
First, install the desired JDKs (e.g., Java 9) using the official dmg installer. After installation the JDK bundles appear under /Library/Java/JavaVirtualMachines: jdk-9.0.4.jdk jdk1.8.0_151.jdk Edit ~/.bash_profile to define home paths and create convenient aliases:
# Java config
export JAVA_8_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home"
export JAVA_9_HOME="/Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home"
# config alias
alias jdk8="export JAVA_HOME=$JAVA_8_HOME"
alias jdk9="export JAVA_HOME=$JAVA_9_HOME"
# set default JDK
export JAVA_HOME=$JAVA_8_HOME
export PATH="$JAVA_HOME:$PATH"Apply the changes with source ~/.bash_profile. Verify the current version using java -version. Switch to Java 9 by running jdk9 and back to Java 8 with jdk8.
Using JEnv for Fine‑Grained Management
Install jenv via Homebrew: brew install jenv Add the jenv initialization lines to ~/.bash_profile:
echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(jenv init -)"' >> ~/.bash_profileAfter reloading the profile, add each JDK to jenv:
jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/
jenv add /Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home/List available versions with jenv versions. Unwanted entries can be removed using jenv remove <name>. Set the global default with jenv global 1.8.0_151 or a project‑specific version with jenv local 1.8.0_151, which creates a .java-version file in the directory.
If jenv doctor reports a pre‑existing JAVA_HOME, comment it out in .bash_profile and reload. Permission errors may disappear after restarting the terminal.
Practical Example in an IDE
Creating a Maven project in IntelliJ IDEA and setting a local JDK version with jenv local temurin64-17.0.4.1 generates a .java-version file containing the version identifier. The project then uses Java 17 without affecting the system‑wide Java 8 configuration.
Conclusion
The article demonstrates two approaches to managing multiple Java versions on macOS: simple alias‑based switching for quick changes, and the more powerful jenv tool for precise, directory‑scoped version control. For most developers, jenv provides the flexibility needed to work with both legacy and modern Java releases.
Senior Brother's Insights
A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
