The cleanest way to manage multiple java versions on Mac is to use Homebrew.

And within Homebrew, use:

  • homebrew-cask to install the versions of java
  • jenv to manage the installed versions of java

As seen on http://hanxue-it.blogspot.ch/2014/05/installing-java-8-managing-multiple.html , these are the steps to follow.

  1. install homebrew
  2. install homebrew jenv
  3. install homebrew-cask
  4. install a specific java version using cask (see "homebrew-cask versions" paragraph below)
  5. add this version for jenv to manage it
  6. check the version is correctly managed by jenv
  7. repeat steps 4 to 6 for each version of java you need

homebrew-cask versions

Add the homebrew/cask-versions tap to homebrew using:

Copybrew tap homebrew/cask-versions

Then you can look at all the versions available by searching for Eclipse temurin, more details here

Copybrew search temurin

Then you can install the version(s) you like, though version 7 no longer shows up in homebrew via these casks:

Copybrew install --cask temurin8
brew install --cask temurin9

And add them to be managed by jenv as usual.

Copyjenv add <javaVersionPathHere>

I think this is the cleanest & simplest way to go about it.


Another important thing to note, as mentioned in Mac OS X 10.6.7 Java Path Current JDK confusing :

For different types of JDKs or installations, you will have different paths

You can check the paths of the versions installed using /usr/libexec/java_home -V, see How do I check if the Java JDK is installed on Mac?

On Mac OS X Mavericks, I found as following:

  1. Built-in JRE default: /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home

  2. JDKs downloaded from Apple: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/

  3. JDKs downloaded from Oracle: /Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home


Resources

  • Removing Java 8 JDK from Mac
  • http://hanxue-it.blogspot.ch/2014/05/installing-java-8-managing-multiple.html
  • http://sourabhbajaj.com/mac-setup/index.html
  • http://brew.sh
  • https://github.com/Homebrew/homebrew/tree/master/share/doc/homebrew#readme
  • http://sourabhbajaj.com/mac-setup/Homebrew/README.html
  • "brew tap” explained https://github.com/Homebrew/homebrew/blob/master/share/doc/homebrew/brew-tap.md
  • “brew versions” explained Homebrew install specific version of formula? and also https://github.com/Homebrew/homebrew-versions
  • https://github.com/caskroom/homebrew-cask
  • “cask versions”, similar to “brew versions”, see https://github.com/caskroom/homebrew-versions and also https://github.com/caskroom/homebrew-cask/issues/9447
  • http://www.jenv.be
  • https://github.com/gcuisinier/jenv
Answer from Adri w Ukraine on Stack Overflow
Top answer
1 of 16
649

The cleanest way to manage multiple java versions on Mac is to use Homebrew.

And within Homebrew, use:

  • homebrew-cask to install the versions of java
  • jenv to manage the installed versions of java

As seen on http://hanxue-it.blogspot.ch/2014/05/installing-java-8-managing-multiple.html , these are the steps to follow.

  1. install homebrew
  2. install homebrew jenv
  3. install homebrew-cask
  4. install a specific java version using cask (see "homebrew-cask versions" paragraph below)
  5. add this version for jenv to manage it
  6. check the version is correctly managed by jenv
  7. repeat steps 4 to 6 for each version of java you need

homebrew-cask versions

Add the homebrew/cask-versions tap to homebrew using:

Copybrew tap homebrew/cask-versions

Then you can look at all the versions available by searching for Eclipse temurin, more details here

Copybrew search temurin

Then you can install the version(s) you like, though version 7 no longer shows up in homebrew via these casks:

Copybrew install --cask temurin8
brew install --cask temurin9

And add them to be managed by jenv as usual.

Copyjenv add <javaVersionPathHere>

I think this is the cleanest & simplest way to go about it.


Another important thing to note, as mentioned in Mac OS X 10.6.7 Java Path Current JDK confusing :

For different types of JDKs or installations, you will have different paths

You can check the paths of the versions installed using /usr/libexec/java_home -V, see How do I check if the Java JDK is installed on Mac?

On Mac OS X Mavericks, I found as following:

  1. Built-in JRE default: /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home

  2. JDKs downloaded from Apple: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/

  3. JDKs downloaded from Oracle: /Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home


Resources

  • Removing Java 8 JDK from Mac
  • http://hanxue-it.blogspot.ch/2014/05/installing-java-8-managing-multiple.html
  • http://sourabhbajaj.com/mac-setup/index.html
  • http://brew.sh
  • https://github.com/Homebrew/homebrew/tree/master/share/doc/homebrew#readme
  • http://sourabhbajaj.com/mac-setup/Homebrew/README.html
  • "brew tap” explained https://github.com/Homebrew/homebrew/blob/master/share/doc/homebrew/brew-tap.md
  • “brew versions” explained Homebrew install specific version of formula? and also https://github.com/Homebrew/homebrew-versions
  • https://github.com/caskroom/homebrew-cask
  • “cask versions”, similar to “brew versions”, see https://github.com/caskroom/homebrew-versions and also https://github.com/caskroom/homebrew-cask/issues/9447
  • http://www.jenv.be
  • https://github.com/gcuisinier/jenv
2 of 16
445

Uninstall jdk8, install jdk7, then reinstall jdk8.

My approach to switching between them (in .profile) :

Copyexport JAVA_7_HOME=$(/usr/libexec/java_home -v1.7)
export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_9_HOME=$(/usr/libexec/java_home -v9)
export JAVA_20_HOME=$(/usr/libexec/java_home -v20)

alias java7='export JAVA_HOME=$JAVA_7_HOME'
alias java8='export JAVA_HOME=$JAVA_8_HOME'
alias java9='export JAVA_HOME=$JAVA_9_HOME'
alias java20='export JAVA_HOME=$JAVA_20_HOME'

#default java8
export JAVA_HOME=$JAVA_8_HOME

Then you can simply type java7 or java8 in a terminal to switch versions.

(edit: updated to add Dylans improvement for Java 9)

🌐
Medium
medium.com › @manvendrapsingh › installing-many-jdk-versions-on-macos-dfc177bc8c2b
Installing & switching between multiple JDK on macOS | by Manvendra P Singh | Medium
February 20, 2023 - Lets use HomeBrew, openjdk and zshrc to install and manage different java / JDK versions on MacOS / OsX
Discussions

How to switch between multiple versions
Hi, Thanks for awesome project! I have installed both openjdk8 and 11, how can I switch between them? More on github.com
🌐 github.com
7
April 26, 2019
How to choose between multiple JDK's on MacOS?
I have used sdkman - you can use it to manage your sdks like java, Scala, groovy More on reddit.com
🌐 r/java
30
22
August 29, 2021
How to set or change the default Java (JDK) version on macOS? - Stack Overflow
I would make a cli tool similar to nvm for switching Java versions, but the acronym jvm already means something else :) 2018-09-22T18:48:10.163Z+00:00 ... I get this error when sourcing: /Users/me/.zshrc:8: permission denied: /Library/Java/JavaVirtualMachines/openjdk-12.jdk/Contents/Home 2019-04-04T22:28:32.053Z+00:00 ... @cosbor11 for .zshrc use export JAVA_HOME=$(/usr/libexec/java_home -v 12) 2019-08-22T12:12:54.087Z+00:00 ... Due to multiple Homebrew... More on stackoverflow.com
🌐 stackoverflow.com
Shall I use Homebrew or SDKMAN to install older versions of Java (e.g. Java 8 or 11)?
I've been using Sdkman for a couple of years. It's a really decent tool. I would recommend it. More on reddit.com
🌐 r/java
35
11
July 6, 2020
🌐
Davidsimpson
davidsimpson.me › 2015 › 08 › 12 › switching-java-versions-easily-on-a-mac
Switching Java versions easily on a Mac - David Simpson
I do a lot of development using Java 7 and recently needed to start using Java 8. The quickest way I know to update to the latest Java uses homebrew: brew tap caskroom/cask brew install brew-cask brew cask install java I now need an easy way to switch back and forth between Java 7 & 8. Adding this to your ~/.bash_profile or ~/.profile does the trick: function setjdk() { if [ $# -ne 0 ]; then removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/bin' if [ -n "${JAVA_HOME+x}" ]; then removeFromPath $JAVA_HOME fi export JAVA_HOME=`/usr/libexec/java_home -v $@` export PATH=$JAVA_HOME/bin:$PATH fi } function removeFromPath() { export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;") } # set your default version setjdk 1.7 – Source.
🌐
Medium
medium.com › @haroldfinch01 › step-by-step-guide-installing-and-switching-java-versions-on-mac-osx-f3896b9872f4
Step-by-Step Guide: Installing and Switching Java Versions on Mac OSX | by Harold Finch | Medium
May 21, 2024 - After installing each version, follow the instructions provided by Homebrew to add the Java versions to your PATH. jenv is a Java environment manager that makes it easy to switch between different versions of Java.
🌐
Snyk
snyk.io › blog › installing-and-managing-java-on-macos
Installing and managing Java on macOS | Snyk
October 12, 2023 - Finally, we can use SDKMAN! to easily switch between Java versions. Running sdk use instructs SDKMAN! to set the active Java version to 19.0.2: ... Now that we’ve reviewed several methods for installing and managing Java on macOS, let's review the answers to some of the most common questions about the process. You can do it manually, use a package manager like Homebrew...
🌐
notiz
notiz.dev › blog › how-to-manage-multiple-java-versions-on-mac
How to manage multiple Java JDK versions on macOS X
April 16, 2020 - The alias exports JAVA_HOME with ... JAVA_HOME=$JAVA_14_HOME' Now, to switch between the Java versions, enter an alias java8 in your terminal....
🌐
GitHub
gist.github.com › tinkerware › 8d92524d78f958f3d821b127393a96a1
Maintaining Java Installs on macOS Using Homebrew Cask · GitHub
############### # Java Switcher ############### alias j8="export JAVA_HOME=`/usr/libexec/java_home -v 1.8`; java -version" alias j10="export JAVA_HOME=`/usr/libexec/java_home -v 10`; java -version" alias j11="export JAVA_HOME=`/usr/libexec/java_home -v 11`; java -version" # Set java 8 as default export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
Find elsewhere
🌐
GitHub
gist.github.com › gwpantazes › 50810d5635fc2e053ad117b39b597a14
How to install different JDK versions on MacOS with Homebrew · GitHub
There is no such thing as a java-lts formula or cask, because there is no single LTS version. You must explicitly choose to install Java 11. Note that this formula is available on homebrew/core, so you don't need to have tapped any caskrooms.
🌐
Mac Install Guide
mac.install.guide › java › brew-cask
Brew Install Java - Easy Cask Method · 2026
Many older tutorials instruct you to run brew tap homebrew/cask-versions before installing versioned Java casks. This tap is deprecated. Modern Homebrew uses the @ syntax directly: ... You now have Java installed and ready to use. If you need to switch between multiple Java versions for different projects, see Java Version Managers to learn about jEnv, SDKMAN, and other tools for switching Java versions.
🌐
Lotharschulz
lotharschulz.info › 2021 › 11 › 30 › how-to-switch-between-java-lts-versions-8-11-and-17-on-mac
How to switch between Java LTS versions 8, 11 and 17 on Mac – Lothar Schulz
Now you can change the java version on the terminal using the respective alias: $ j17 openjdk version "17.0.1" 2021-10-19 OpenJDK Runtime Environment Homebrew (build 17.0.1+0) OpenJDK 64-Bit Server VM Homebrew (build 17.0.1+0, mixed mode, sharing) $ j11 openjdk version "11.0.12" 2021-07-20 OpenJDK Runtime Environment Homebrew (build 11.0.12+0) OpenJDK 64-Bit Server VM Homebrew (build 11.0.12+0, mixed mode) $ j1.8 openjdk version "1.8.0_312" OpenJDK Runtime Environment (build 1.8.0_312-bre_2021_10_20_23_15-b00) OpenJDK 64-Bit Server VM (build 25.312-b00, mixed mode)
🌐
DEV Community
dev.to › notiz_dev › how-to-manage-multiple-java-jdk-versions-on-macos-x-41mi
How to manage multiple Java JDK versions on macOS X - DEV Community
April 27, 2020 - The alias exports JAVA_HOME with ... JAVA_HOME=$JAVA_14_HOME' Now, to switch between the Java versions, enter an alias java8 in your terminal....
🌐
Mr N
bigsoft.co.uk › blog › 2025 › 11 › 07 › installing-all-the-javas-on-a-mac-and-switching-between-them
Installing all the Javas on a Mac and switching between them
November 7, 2025 - $ jenv global 21 $ echo $JAVA_HOME /Users/mrn/.jenv/versions/21 $ java -version openjdk version "21.0.9" 2025-10-21 OpenJDK Runtime Environment Homebrew (build 21.0.9) OpenJDK 64-Bit Server VM Homebrew (build 21.0.9, mixed mode, sharing) All without having to restart the shell! The more observant of you will notice I used the keyword global. This sets to the default version. You can also use the keyword local which fixes the java version for this directory and below. This is great for when you have different Java projects all using different versions of Java or you need to easily switch java version when compatibility testing.
🌐
DevQA
devqa.io › switch-between-different-java-versions-mac
How to Switch Between Different Java Versions on Mac
July 4, 2023 - 1 - Install jenv using Homebrew by running the following command in Terminal: ... 2 - Once the installation is complete, you need to initialize jenv. Run the following command to add jenv to your shell: ... If you’re using a different shell (such as Zsh), replace ~/.bash_profile with the appropriate configuration file (e.g., ~/.zshrc). 3 - Restart your Terminal or run the following command to apply the changes: ... 4 - Now, you can install different versions of Java using Homebrew.
🌐
GitHub
github.com › AdoptOpenJDK › homebrew-openjdk › issues › 111
How to switch between multiple versions · Issue #111 · AdoptOpenJDK/homebrew-openjdk
April 26, 2019 - You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window.
Author   AdoptOpenJDK
🌐
MungingData
mungingdata.com › java › jenv-multiple-versions-java
Running Multiple Versions of Java on MacOS with jenv - MungingData
It also makes it easy to seamlessly ... when you switch projects. Running multiple Java versions is important for Android and Apache Spark developers. Spark developers should use Java 8 for Spark 2 projects and Java 11 for Spark 3 projects for example. This blog post shows you how to get jenv setup on your computer and how to use the important commands. Install jenv with brew install jenv. This is a Homebrew ...
🌐
Reddit
reddit.com › r/java › how to choose between multiple jdk's on macos?
r/java on Reddit: How to choose between multiple JDK's on MacOS?
August 29, 2021 -

On MacOS, you can run /usr/libexec/java_home to get the current JDK MacOS will use...

On my system, it's:

OpenJDK Runtime Environment Zulu16.32+15-CA (build 16.0.2+7)

But, I have multiple JDK's installed:

/usr/libexec/java_home -V

Matching Java Virtual Machines (2):

16.0.2 (arm64) "Azul Systems, Inc." - "Zulu 16.32.15" /Library/Java/JavaVirtualMachines/zulu-16.jdk/Contents/Home

16.0.2 (x86_64) "Oracle Corporation" - "Java SE 16.0.2" /Library/Java/JavaVirtualMachines/jdk-16.0.2.jdk/Contents/Home

Does anyone know how (if I can) switch what MacOS will use by default?

So far, if I delete the current default, it'll switch to the one remaining (I've tried this my moving the current JDK to /tmp). But is there a more elegant way to do this?

Top answer
1 of 16
2562

First run /usr/libexec/java_home -V which will output something like the following:

Matching Java Virtual Machines (3):
1.8.0_05, x86_64:   "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home
1.6.0_65-b14-462, x86_64:   "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
1.6.0_65-b14-462, i386: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home

Pick the version you want to be the default (1.6.0_65-b14-462 for arguments sake) then:

export JAVA_HOME=`/usr/libexec/java_home -v 1.6.0_65-b14-462`

or you can specify just the major version, like:

export JAVA_HOME=`/usr/libexec/java_home -v 1.8`

Now when you run java -version you will see:

java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)

Add the export JAVA_HOME… line to your shell’s init file.

For Bash (as stated by antonyh):

export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)

For Fish (as stated by ormurin)

set -x JAVA_HOME (/usr/libexec/java_home -d64 -v1.8)

Updating the .zshrc file should work:

nano ~/.zshrc

export JAVA_HOME=$(/usr/libexec/java_home -v 1.8.0)

Press CTRL+X to exit the editor Press Y to save your changes

source ~/.zshrc
echo $JAVA_HOME
java -version
2 of 16
608

This answer is an attempt to address: how to control java version system-wide (not just in currently running shell) when several versions of JDK are installed for development purposes on macOS El Capitan or newer (Sierra, High Sierra, Mojave). As far as I can tell, none of the current answers do that (*).

As a developer, I use several JDKs, and I want to switch from one to the other easily. Usually I have the latest stable one for general use, and others for tests. But I don't want the system (e.g. when I start my IDE) to use the latest "early access" version I have for now. I want to control system's default, and that should be latest stable.

The following approach works with Java 7 to 12 at least (early access at the time of this writing), with Oracle JDK or OpenJDK (including builds by AdoptOpenJDK produced after mid-October 2018).

Solution without 3rd party tools:

  • leave all JDKs at their default location, under /Library/Java/JavaVirtualMachines. The system will pick the highest version by default.
  • To exclude a JDK from being picked by default, rename its Contents/Info.plist to Info.plist.disabled. That JDK can still be used when $JAVA_HOME points to it, or explicitly referenced in a script or configuration. It will simply be ignored by system's java command.

System launcher will use the JDK with highest version among those that have an Info.plist file.

When working in a shell with alternate JDK, pick your method among existing answers (jenv, or custom aliases/scripts around /usr/libexec/java_home, etc).


Details of investigation in this gist.


(*) Current answers are either obsolete (no longer valid for macOS El Capitan or Sierra), or only address a single JDK, or do not address the system-wide aspect. Many explain how to change $JAVA_HOME, but this only affects the current shell and what is launched from there. It won't affect an application started from OS launcher (unless you change the right file and logout/login, which is tedious). Same for jenv, it's cool and all, but as far as I can tell it merely changes environment variables, so it has the same limitation.

🌐
DevQA
devqa.io › brew-install-java
How to Use Brew to Install Java on Mac
In this article we show how to install Java on Mac using Homebrew, and how to allow to switch between different versions such as Java8, Java11, Java13 and latest Java version.
🌐
javathinking
javathinking.com › blog › how-do-i-install-java-on-mac-osx-allowing-version-switching
How to Install OpenJDK on macOS Using Homebrew Cask: Enable Version Switching Between Multiple JDKs — javathinking.com
Verify the JDK path with /usr/libexec/java_home -v <version>. Re-add the JDK to jenv: jenv add /path/to/jdk. With Homebrew Cask, installing and managing OpenJDK on macOS is straightforward. By combining Homebrew with jenv, you can seamlessly switch between JDK versions to support diverse project requirements.