How to Manage Multiple Java Versions in MacOS (Updated 2021 + macOS Big Sur)

Java

I have been using multiple versions of Java with a Version Manager in my Mac for a while, whenever I need to switch between different projects which use different versions of Java this feature becomes handier, so that I thought of sharing that with you how you can use a Java Version Manager-jEnv in your daily work in order to switch between multiple Java versions.

Overview of Java JDK flavors

Alright; I hope you have already used or been using Java in your machine, if yes; you probably aware that we have two main flavors of JDK in the market; if you don’t know here are the main two:

  1. Open JDK
  2. Oracle JDK

Apart from the above main two, there are other different flavors which maintain by different companies and open source communities. Here you can see a few of them listed under the section “OpenJDK builds”, Among those AdoptOpenJDK becoming more popular among the Java community.

Key differences:

1. The biggest difference in OpenJDK vs. Oracle JDK is in licensing. OpenJDK is completely open source while Oracle JDK requires a commercial license.

2.. Oracle will deliver releases every three years, while OpenJDK will be released every six months.

3. Oracle provides long term support for its releases. On the other hand, OpenJDK supports the changes to a release only until the next version is released.

4. Oracle JDK was licensed under Oracle Binary Code License Agreement, whereas OpenJDK has the GNU General Public License (GNU GPL) version 2 with a linking exception.

5. Oracle product has Flight Recorder, Java Mission Control, and Application Class-Data Sharing features, while OpenJDK has the Font Renderer feature. Also, Oracle has more Garbage Collection options and better renderers, etc.

6. Oracle JDK is fully developed by Oracle Corporation whereas the OpenJDK is developed by Oracle, OpenJDK, and the Java Community. However, the top-notch companies like Red Hat, Azul Systems, IBM, Apple Inc., SAP AG also take an active part in its development.

Installing Dependencies

Before getting into work with multiple Java versions in your machine, you need to do a fair bit of preparations installing few modules to your mac OS;

Homebrew:

As the homebrew advertising homebrew is the tool that does this: “Homebrew installs the stuff you need that Apple (or your Linux system) didn’t.”

Installing Homebrew is pretty straight forward, copy paste and run the command below in your terminal:

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

The latest version of the Homebrew will install a package called “Homebrew Cask”, Cask offers a way to do the command line manage the installations of graphical applications.

Homebrew Cask advertises as “To install, drag this icon…” no more!

Cool! Now we set up the free MacOs package manager into our system, next step is to Install the Java Version Manger.

Installing Java Version Manager - jEnv

As I mentioned above this tool lets you switch between multiple Java JDK versions without a pain.

As the jEnv advertise “jEnv is a command-line tool to help you forget how to set the JAVA_HOME environment variable”

Alright, earlier we installed our mac OS package manager Homebrew, now we are going to make use of to install the jEnv for us. To do that simply copy and paste the following command to your terminal:

brew install jenv

Once you installed jEnv into your mac, you will see an out as follows:

jEnv install log

Once you installed jEnv into the machine successfully, next you need to add the following to your shells configuration file. Based on the shell you use you need to paste them in the correct file. For bash that would typically be ~/.bash_profile or ~/.bashrc , for zsh it's ~/.zshrc . You can see that is the above output’s Caveats section, with your shell type. In my case, it’s zsh.

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

In order to paste the above lines in your shell configuration file either you can use your text editor (i.e vim, nano, etc) or you can echo as follows to your shell configuration file. You should change the shell configuration file name based on the shell type you use.

echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(jenv init -)"' >> ~/.zshrc

So if you were able to update your shell configuration file successfully your file should look as follows:

.zshrc file

Once you are done with above steps restart your terminal application or resource your configuration. To do that you can use the following command (make sure to change the file name based on your shell type):

source ~/.zshrc

You should now be able to run jenv doctor in terminal to verify your installation of jEnv.

If you see an output similar to above you are in luck. Ignore all the errors and warnings that you see, if you are seeing Jenv is correctly loaded message on your terminal all good for now.

But there are a couple of more things you want to do before going to the next step; that’s Run the following commands in the terminal if you are using maven(i.e mvn):

# ensure that JAVA_HOME is correct
jenv enable-plugin export
# make Maven aware of the Java version in use (and switch when your project does)
jenv enable-plugin maven

All good! Now we are fully configured to use jEnv on our mac, What’s remaining is to install different JDK versions that we wish to switch between. In the next section, we shall look into it.

Installing different JDK versions

As I explained earlier there are two main flavors of JDK versions, Here I’m going to make use of Open JDK as it’s free and open source. Earlier we installed Homebrew to mage our packages in the Mac OS, so we going to make use of Homebrew Cask in order to install different versions of Java.
Homebrew will install the Open JDK version of Java. To install a few versions follow the following steps:

brew cask install java

If you see an error as follows:

Try this command instead:

brew install --cask java

Once you installed the java latest version you will see an output as below

Java Open SDK Install console output

Even if you failed with the second command as follows:

That may be because java has been moved into brew core recently, so the correct command as of January 2021 is:

brew install java

Now you should be able to install Java as follows successfully.

For older Java versions we need to specify the version with Homebrew Cask which version to install, let’s install a couple of them also. I’ll be using AdoptOpenJDK which is also a flavor of Open JDK

brew cask install AdoptOpenJDK/openjdk/adoptopenjdk{8,11}

if that didn’t work and if you are running this command after January 2021, the command is:

brew install AdoptOpenJDK/openjdk/adoptopenjdk{11,14}# or you can install a single version as follows:brew install AdoptOpenJDK/openjdk/adoptopenjdk8

Then you should probable see an output as follows:

I hope you have installed any java versions you liked. Next is to configure jEnv to use installed JDK versions.

Configuring JDK Versions with jEnv

Now you are at the final step of configuring all the installed JDK versions with jEnv in order to switch between different Java versions. We basically need to tell jEnv where are the JDK versions we want it to manage, that’s what we are going to do in this section.

To do that we need to find all of our JDK root paths. You can hit the following command in the command line to see all the installed JDK versions in your machine.

/usr/libexec/java_home -V

It will list all of your installed JDK versions as follows:

All JDK versions list

Now you can add to jEnv each of the JDK you installed by the following command:


Syntax:
jenv add <your_jdk_path>Example:
jenv add /Library/Java/JavaVirtualMachines/openjdk-14.0.1.jdk/Contents/Home

If you are successful in the above operation, you will see an output as below:

jEnv add JDK

Now you can add each of the listed JDKs using the above command to jEnv. Once you add all the JDKs to jEnv you can list and see whether all the versions you add are available in the jEnv using the following command.

jenv versions

You can see an input as follows:

JDK versions in jEnv

jEnv is now ready for use in daily development workflows. Few things you want to know before using jEnv;

Setting system-wide Java version:

jenv global 14.0

Setting project-wide Java version:

If a specific project needs a different version of Java just hit the following command in the terminal by standing in the directory of that project. jEnv will then create a .java-version file that describes which JDK to use for. This file can safely be checked in so that your whole team runs the same version of Java (if using jEnv of course).

jenv local 11

Setting shell instance Java version:

If you need to run a different version of Java in your shell(i.e terminal window), you can run the following command.

jenv shell openjdk64-1.8.0.252

Checking current Java Version

java -version

More info: https://www.jenv.be/

Hope I covered everything related to using Java Version Manager — jEnv in your mac, feel free to ask any questions in the comment section. If you have any. Cheers!!

Also you can following articles as well for managing multiple versions of Node and Python in MacOS

How to Manage Multiple Node Versions in MacOS

How to Manage Multiple Python Versions in MacOS

Versatile Full-stack Developer with 5+ years of experience designing, developing, and managing complex applications and internal frameworks.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store