Install and Switch multiple versions of OpenJdk on Mac OS

While working on enterprise level, we often have java projects running on older versions of java for legacy reasons. Hence it is not very uncommon for a developer to have multiple versions of JDK installed in their machines and easy way to switch different versions of JDK on demand for running the build. This blog explains the steps needed to setup and configure multiple JDK versions on Mac OS.

Download and extract multiple OpenJdk from Adapt OpenJdk


$ wget "https://github.com/AdoptOpenJDK/openjdk8-binaries/releases/download/jdk8u212-b04/OpenJDK8U-jdk_x64_mac_hotspot_8u212b04.tar.gz" -P /Users/macuser/openjdk

$ tar -xvzf /Users/macuser/openjdk/OpenJDK8U-jdk_x64_mac_hotspot_8u212b04.tar.gz -C /Users/macuser/openjdk

$ wget "https://github.com/AdoptOpenJDK/openjdk11-binaries/releases/download/jdk-11.0.3%2B7/OpenJDK11U-jdk_x64_mac_hotspot_11.0.3_7.tar.gz" -P /Users/macuser/openjdk

$ tar -xvzf /Users/macuser/openjdk/OpenJDK11U-jdk_x64_mac_hotspot_11.0.3_7.tar.gz -C /Users/macuser/openjdk

Configure alias and environment variables for bash shell or zsh shell


Add the following lines to ~/.bash_profile or ~/.zprofile

# setting JAVA_HOME

export JAVA_8_HOME=/Users/macuser/openjdk/jdk8u212-b04/Contents/Home

export JAVA_11_HOME=/Users/macuser/openjdk/jdk-11.0.3+7/Contents/Home

# setting alias to switch java version

alias java8='export JAVA_HOME=$JAVA_8_HOME && export PATH=$JAVA_8_HOME/bin:$PATH'

alias java11='export JAVA_HOME=$JAVA_11_HOME && export PATH=$JAVA_11_HOME/bin:$PATH'

# setting default version to 8

export JAVA_HOME=$JAVA_8_HOME

export PATH=$JAVA_8_HOME/bin:$PATH


Switching different versions of java using alias


Open a new terminal (or restart any existing terminals)

verify default java version

$ java -version
$ echo $JAVA_HOME

switch to java 11

$ java11
$ java -version
$ echo $JAVA_HOME

switch back to java 8

$ java8
$ java -version
$ echo $JAVA_HOME

Comments

Popular posts from this blog

JSON with curl and jq

Import self signed in Linux for Chrome / Chromium headless testing

Colima - Drop In replacement for Docker Desktop for Mac and Linux