Installing and Switching Java Versions On A Mac
With the relatively rapid change schedule for Java nowadays, I find it useful to have several versions of the JDK installed on my Mac so that I can easily switch between versions depending upon need.
I use the AdoptOpenJDK binaries and install them via HomeBrew.
Installation
To install via Homebrew, execute the following command (after installing HomeBrew itself which I'll not discuss here):
brew tap AdoptOpenJDK/openjdk
brew cask install <version>
Version can be one of:
adoptopenjdk8
adoptopenjdk9
adoptopenjdk10
adoptopenjdk11
adoptopenjdk12
adoptopenjdk13
In addition to these "standard" builds, you can install AdoptOpenJDK builds with the OpenJ9 JVM by adding the suffix -openj9
, for example:
brew tap AdoptOpenJDK/openjdk
brew cask install adoptopenjdk13-openj9
You can get a full list of JDK and JRE versions that can be installed from the ( Project Page.
Selecting JDK Version
After installing the different versions of the JDK, you can easily add a zshrc (or bash) alias to allow a different version of the JDK to be selected. This can be achieved by running the following command to select a different Java version.
> echo 'alias java8="export JAVA_HOME=`/usr/libexec/java_home -v 1.8`; java -version"' >> ~/.zshrc
> echo 'alias java11="export JAVA_HOME=`/usr/libexec/java_home -v 11`; java -version"' >> ~/.zshrc
This example installs aliases for Java 8 and 11, but you can see how it would be easy to expand this for different versions of Java.
Credits
Photo by Tim Foster on Unsplash
No Comments Yet