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 ~/.zshrc

Add following entries in ~/.zshrc file, save and exit the editor using Ctrl + O then ENTER and Ctrl + X then ENTER key combination. On older versions of MacOS, replace .zshrc with .profile.

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 ~/.zshrc

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. Please note that on older versions of MacOS following command will revsied as “brew cask install adoptopenjdk8“.

brew install --cask adoptopenjdk8

Next install Open JDK version 11 using following command.

brew install --cask 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 install --cask adoptopenjdk

Configure JDK version with jEnv

Now we have Java 8, 11 and 16 (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):
    16 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 16" /Library/Java/JavaVirtualMachines/adoptopenjdk-16.jdk/Contents/Home
    11.0.11 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 11" /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home
    1.8.0_292 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 8" /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/adoptopenjdk-16.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-16.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.292
  11
  11.0
  11.0.11
  16
  openjdk64-1.8.0.292
  openjdk64-11.0.11
  openjdk64-16

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 16

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 "16" 2021-03-16
OpenJDK Runtime Environment AdoptOpenJDK (build 16+36)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 16+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_292"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_292-b10)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.292-b10, mixed mode)

Set local 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 16

Set Shell specific Java using jEnv

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

jenv shell 11

Shoket Mahmood Ahmed

109 thoughts on “How to manage multiple Java versions on MacOS

  1. I am really loving the theme/design of your weblog. Do you ever run into any browser compatibility problems? A number of my blog visitors have complained about my blog not operating correctly in Explorer but looks great in Firefox. Do you have any recommendations to help fix this issue? Sherill Brnaba Homans

  2. An attention-grabbing discussion is price comment. I feel that you should write extra on this matter, it may not be a taboo topic however generally individuals are not sufficient to talk on such topics. To the next. Cheers Bea Wolfy Arlon

  3. This is such an informative article. I have been searching for out more concerning this subject and also stopping working. Thank benefits I determined to browse once again and discovered you. Congratulations on a superb message as well as I will certainly be reading a lot more. Anabelle Guntar Ranie

  4. In its default setting, your Casino will highlight incoming messages on its lock screen. This is something that some find convenient, but others do not like so much. If you are one that does not like that feature, you will be happy to learn that you can stop that feature from continuing. Simply go to the Notifications section of your settings menu and choose messages. You can then turn off the Show Preview option. Cynthia Griffin Selima

  5. Hey there! I just wish to offer you a huge thumbs up for the excellent information you have right here on this post. I will be returning to your web site for more soon. Anica Rutledge Cordula

  6. Usually I do not learn article on blogs, however I would like to say that this write-up very pressured me to check out and do it! Your writing style has been surprised me. Thanks, quite nice post. Halley Saxon Hillery

  7. In the audience tonight with friends and family: loved it! What a great little script full of gut wrenching emotions at times but then with some just brilliant one-liner responses that guaranteed audience engagement! Kaitlin Bobbie Carmelina

  8. I do not know whether it’s just me or if perhaps everybody else experiencing problems with your blog.
    It seems like some of the written text in your content are running off the screen. Can someone else please
    comment and let me know if this is happening to them too?
    This could be a problem with my browser because I’ve had this happen before.
    Kudos

Leave a Reply

Back to top