How to manage multiple Java JDK versions on macOS X

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

Authors
Marc Stammerjohann Marc Stammerjohann
Published at

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
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.

Alias **only** changes the Java version in the used terminal instance

Sponsor us

Did you find this post useful? We at notiz.dev write about our experiences developing Apps, Websites and APIs and develop Open Source tools. Your support would mean a lot to us 🙏. Receive a reward by sponsoring us on Patreon or start with a one-time donation on GitHub Sponsors.

Table of Contents

Top of Page Comments Related Articles

Related Posts

Find more posts like this one.

Authors
Gary Großgarten
June 08, 2020

Create Keyboard Shortcuts with RxJS

The cleanest way to create and orchestrate Keyboard Shortcuts with RxJS.
RxJS Quick Tip Read More
Authors
Marc Stammerjohann
April 26, 2020

Migrate Git Repository to Git Large File Storage (LFS)

How to migrate an existing Git Repository to use Git Large File Storage (LFS).
Quick Tip Git Read More
Authors
Marc Stammerjohann
March 19, 2020

How to manage multiple Node.js versions on macOS X

How to manage multiple Node.js versions on macOS X using homebrew.
Quick Tip Node Read More

more coming soon

Get in Touch!

Sign up for our newsletter

Sign up for our newsletter to stay up to date. Sent every other week.

We care about the protection of your data. Read our Privacy Policy.