How to manage multiple Java versions on MacOS

In this article we will explorer how to install and manage multiple Java version on MacOS. For this purpose, we will use a well known Java Version Manager software called jEnv.

There are two Java releases

FeatureOracle JDKOpen JDK
Licensing
Commercial: Oracle Binary Code License Agreement
Open source: GNU GPL v 2
Release CycleEvery three years (LTS)Every three months
Other FeaturesFlight Recorder
Java Mission Control
Application Class-Data Sharing
Better Garbage Collection
Font Renderer
ContributorsOracleOracle
Red Hat
Azul Systems
IBM
Apple Inc.
SAP AG

We will use Homebrew (the package manager for MacOS) to install the required softwares, open a terminal and run following command to install homebrew on your Mac.

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)

Optional: Remove Existing Java

If you have any existing version of java installed on your machine, open terminal run following commands on you Mac.

sudo rm -rf /Library/Java/*
sudo rm -rf /Library/PreferencePanes/Java*
sudo rm -rf /Library/Internet\ Plug-Ins/Java*

We will make use of Homebrew Cask (homebrew package to install GUI based applications), cask package is included by default in latest versions of homebew so we not need to install it separately.

Install Java version manager: jEnv

jEnv, a well known Java Version Manager is a command line tool to manage multiple versions of java on your machine, behind the scenes it uses JAVA_HOME environment variable to do this.

Open a terminal window and run following command to install jEnv on your Mac machine.

brew install jenv

Output of successful installation will be as following.

Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/cask).
==> Updated Casks
brave-browser

==> Downloading https://github.com/jenv/jenv/archive/0.5.4.tar.gz
==> Downloading from https://codeload.github.com/jenv/jenv/tar.gz/0.5.4
######################################################################## 100.0%
==> Caveats
To activate jenv, add the following to your ~/.profile:

  export PATH="$HOME/.jenv/bin:$PATH"
  eval "$(jenv init -)"
==> Summary
🍺  /usr/local/Cellar/jenv/0.5.4: 82 files, 72.1KB, built in 2 seconds

Now we have jEnv installed on our machine and will add following entries to shell configuration. Location and name of the shell configuration may vary depending on the currently active shell on your Mac. If you look at the output from last section it actually has the reference to the target shell configuration file i,e. ~/.profile. So let’s open it with nano text editor.

sudo nano ~/.profile

Add following entries in ~/.profile file, save and exit the editor using Ctrl + O then ENTER and Ctrl + X then ENTER key combination.

export PATH="$HOME/.jenv/bin:$PATH"
eval "$(jenv init -)"

jenv enable-plugin export
jenv enable-plugin maven

Now either restart the Terminal or type following command to reload the shell configurations.

source ~/.profile

Verify the installation of jEnv by running following command.

jenv doctor

Successful installation will produce output like following.

OK]	No JAVA_HOME set
[ERROR]	Java binary in path is not in the jenv shims.
[ERROR]	Please check your path, or try using /path/to/java/home is not a valid path to java installation.
	PATH : /usr/local/Cellar/jenv/0.5.4/libexec/libexec:/Users/u1/.jenv/shims:/Users/sma/.jenv/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
[OK]	Jenv is correctly loaded

We will activate AdoptOpenJDK tap to install specific version of Open JDK, to do this from terminal run following command.

brew tap AdoptOpenJDK/openjdk

Now let’s start with installing Java 8.

brew cask install adoptopenjdk8

Next install Open JDK version 11 using following command.

brew cask install adoptopenjdk11

For successful installation the output will be something like following.

Updating Homebrew...
==> Downloading https://github.com/AdoptOpenJDK/openjdk11-binaries/releases/down
Already downloaded: /Users/sma/Library/Caches/Homebrew/downloads/1d0d41e7cf8f14ebb4df0ff39fa16e0fe2a1ac85f05ae404c62841c6ff01d4c8--OpenJDK11U-jdk_x64_mac_hotspot_11.0.8_10.pkg
==> Verifying SHA-256 checksum for Cask 'adoptopenjdk11'.
==> Installing Cask adoptopenjdk11
==> Running installer for adoptopenjdk11; your password may be necessary.
==> Package installers may write to any location; options such as --appdir are i
installer: Package name is AdoptOpenJDK
installer: Upgrading at base path /
installer: The upgrade was successful.
package-id: net.adoptopenjdk.11.jdk
version: 11.0.8+10
volume: /
location: Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk
install-time: 1602892594
🍺  adoptopenjdk11 was successfully installed!

Let’s also install the latest available version of java using following command.

brew cask install adoptopenjdk

Configure JDK version with jEnv

Now we have Java 8, 11 and 15 (latest available version on publish date of this article) installed on our machine.

In this section will learn to configure current JDK version with the help of jEnv.

To enable jEnv switch between different java versions, we need to tel jEnv about all versions installed on our machine. Tun following command to get a list of all versions of java installed on your machine.

/usr/libexec/java_home -V

This will return list of all installed java versions as following.

Matching Java Virtual Machines (3):
    15, x86_64:	"AdoptOpenJDK 15"	/Library/Java/JavaVirtualMachines/adoptopenjdk-15.jdk/Contents/Home
    11.0.8, x86_64:	"AdoptOpenJDK 11"	/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home
    1.8.0_265, x86_64:	"AdoptOpenJDK 8"	/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/adoptopenjdk-15.jdk/Contents/Home

Now we will add each of the installed JDK to jEnv as following, please do remember to revise the commands according to path of versions installed on your machine.

jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-15.jdk/Contents/Home
jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home
jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home

Let’s verify if all of the Java versions are now available to jEnv by running following command.

jenv versions

This will produce output like following.

* system (set by /Users/sma/.jenv/version)
  1.8
  1.8.0.265
  11
  11.0
  11.0.8
  15
  openjdk64-1.8.0.265
  openjdk64-11.0.8
  openjdk64-15

Congratulations, you have setup jEnv successfully and can use it to manage your development environments easily.

Set global Java using jEnv

We can use following command to set global / system wide java version.

jenv global 15

Above command will set Java 15 as the system wide Java version, let’s verify it by running java -version command as following.

java -version

Following output confirms current java version as Java 15.

openjdk version "15" 2020-09-15
OpenJDK Runtime Environment AdoptOpenJDK (build 15+36)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 15+36, mixed mode, sharing)

Let’s switch our global version to Java 8 by running following command.

jenv global 1.8

Let’s confirm the change by running following command.

java -version

And output confirms that now the global java version is Java 1.8.

openjdk version "1.8.0_265"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_265-b01)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.265-b01, mixed mode)

Set global Java using jEnv

Use following command to set project specific Java version, this will create a file named .java-version and you can even commit this file in source control to share it with team.

jenv local 15

Set Shell specific Java using jEnv

We can set Java version for currently opened shell using following command.

jenv shell 11

Shoket Mahmood Ahmed

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top