Here is a quick tip on how to install multiple Java JDK versions (8, …, 11, …, 14 etc.) on macOS X and how to switch between them for your applications.
Installing Java JDK via Homebrew
Install multiple Java JDK versions using Homebrew. To install Homebrew run:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
Now install the Java JDK version 11 or above using
brew cask install java<version> # latest version brew cask install java # LTS 11 brew cask install java11
Note JDK versions prior 11 ( 8 , 9 and 10 ) are no longer supported.
AdoptOpenJDK provides older Java versions. To install the Java JDKs from AdoptOpenJDK:
# install from third party repository brew tap adoptopenjdk/openjdk brew cask install adoptopenjdk<version> # Java 8 brew cask install adoptopenjdk8 # Java 9 brew cask install adoptopenjdk9 # Java 10 brew cask install adoptopenjdk10
Switch Java JDK via alias
JAVA_HOME path in your
.bash_profile for your primary Java version and add an export for each installed Java version.
export JAVA_HOME=$(/usr/libexec/java_home -v14) export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8) export JAVA_11_HOME=$(/usr/libexec/java_home -v11) export JAVA_14_HOME=$(/usr/libexec/java_home -v14)
To check the default Java version and installation path:
java -version # 14
Add an alias to your
.bash_profile for each installed Java version. The alias exports
JAVA_HOME with the selected
alias java8='export JAVA_HOME=$JAVA_8_HOME' alias java11='export JAVA_HOME=$JAVA_11_HOME' alias java14='export JAVA_HOME=$JAVA_14_HOME'
Now, to switch between the Java versions, enter an alias
java8 in your terminal. Execute
java -version to verify that you are now using the correct Java version.
Note : Alias only changes the Java version in the used terminal instance
Top comments (3)
Thank you for this recent tutorial on how to manage Java versions with Homebrew. Most other guides I tried to follow are written for outdated
Quick warning: This does NOT seem to work if you have Java installed in some way through
conda. I had
openjdkinstalled in my
baseenv (it was required by another program installed with conda) and I could not get this solution to work until I uninstalled
openjdkfrom my active
condaenv or switched to an env without
openjdkinstalled. I'm sure it was a problem with my
PATH, but since I couldn't figure it out after a while, I just made a new conda env with the tools that automatically install their own
openjdkfrom my active
condabase env. Everything seems to work great now, thanks a ton!
Edit: I seem to be running into a problem where the command
export JAVA_HOME=[...]in my
~/.zshrcdoesn't seem to actually set
JAVA_HOME. E.g. as soon as I start a new shell window and type
echo $JAVA_HOME, nothing appears and upon inspection of variables listed by
JAVA_HOMEdoes not appear. Interestingly, if I type
java -version, it does show my
adoptopenjdk14as the current version even though the variable
JAVA_HOMEdoesn't exist. Any idea why? It does set itself when I run one of the aliases, but that requires manually running the alias.
Edit 2: I found the problem: It was because I still had some code in my
.zshrcfrom trying a previous solution.
Hey Marc, you should check out SDKMAN!, you can easily switch between (but also install and uninstall) different Java (and Scala, and Gradle, and Kotlin, Groovy, sbt, Spring Boot, ...) versions using SDKMAN! It's seriously amazing, and really easy to use.
Hi Andrew, thanks for the tip! I'll it try out!