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

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

  1. No matter if some one searches for his essential thing, thus he/she wishes to be available that in detail, thus that thing is maintained over here. Barbe Renault Winfrid

  2. Useful information. Lucky me I found your site by accident, and I am shocked why this accident did not came about earlier! I bookmarked it. Kalila Gearard Strickler

  3. This article provides clear idea designed for the new viewers of blogging, that really how to do blogging and site-building. Katheryn Mathias Einberger

  4. Outstanding post, I conceive people should acquire a lot from this weblog its real user friendly. So much fantastic information on here . Tamqrah Napoleon Ruzich

  5. I think the admin of this web page is truly working hard in favor of his website, as here every material is quality based data. Pauletta Hector Sayer

  6. Mi dui, tincidunt id venenatis vel, luctus quis lorem. Curabitur a ante non leo eleifend pretium. Vivamus efficitur ex varius dignissim imperdiet. Ethelind Lesley Gherlein

  7. I have recently started a website, the info you offer on this site has helped me greatly. Thank you for all of your time & work. Molly Saw Arnoldo

  8. I just like the helpful info you supply to your articles. I will bookmark your blog and take a look at once more here regularly. Trudie Raleigh Tiffanle

  9. I am actually happy to read this webpage posts which consists of lots of helpful information, thanks for providing these kinds of data. Lauryn Vittorio Atalanti

  10. As the admin oof this website is working, no question very shortly itt will be renowned, due tto its quality contents. Irita Gearard Olatha

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

  12. Great blog here! Also your site loads up fast! What web host are you using? Can I get your affiliate link to your host? I wish my web site loaded up as quickly as yours lol Patrica Horten Eal

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

  14. Everything is very open with a really clear description of the issues. It was truly informative. Your website is useful. Thank you for sharing! Ariana Tarrance Erving

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

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

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

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

  19. Thanks , I have just been looking for information about this subject for ages and yours is the best I have found out so far. However, what about the bottom line? Are you sure concerning the source? Cindra Morton Pence

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

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

  22. Hey there! I’ve been following your weblog for a while now and finally got the bravery to go ahead
    and give you a shout out from Kingwood Texas! Just wanted to mention keep
    up the great work!

Leave a Reply

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

Back to top