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
:
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
Setup your JAVA_HOME
path in your .zshrc
or .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 .zshrc
or .bash_profile
for each installed Java version. The alias exports JAVA_HOME
with the selected JAVA_VERSION_HOME
.
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
brew
functionality.Quick warning: This does NOT seem to work if you have Java installed in some way through
conda
. I hadopenjdk
installed in myconda
base
env (it was required by another program installed with conda) and I could not get this solution to work until I uninstalledopenjdk
from my activeconda
env or switched to an env withoutopenjdk
installed. I'm sure it was a problem with myPATH
, 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 ownopenjdk
and uninstalledopenjdk
from my activeconda
base 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~/.zshrc
doesn't seem to actually setJAVA_HOME
. E.g. as soon as I start a new shell window and typeecho $JAVA_HOME
, nothing appears and upon inspection of variables listed byenv
,JAVA_HOME
does not appear. Interestingly, if I typejava -version
, it does show myadoptopenjdk14
as the current version even though the variableJAVA_HOME
doesn'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
.zshrc
from 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!