notiz/blog/How to manage multiple Java JDK versions on macOS X
October 23, 2020

How to manage multiple Java JDK versions on macOS X

How to manage multiple Java JDK versions on macOS X using homebrew.

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

Note JDK versions 8, 9, 10 and 11 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

# Java 11
brew cask install adoptopenjdk11

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

Author

Weeklyish Newsletter

Stay up to date with Angular, NestJS and more! Sign up to receive updates on our latest posts.

Blog