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
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
Source: https://dev.to/notiz_dev/how-to-manage-multiple-java-jdk-versions-on-macos-x-41mi