How to Manage Multiple Java Versions in MacOS (Updated 2023 + macOS Ventura)

I have been using multiple versions of Java with a Version Manager on my Mac for a while, whenever I need to switch between different projects which use different versions of Java this feature becomes handier so I thought of sharing 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 on your machine, if yes; you are probably aware that we have two main flavors of JDK in the market; if you don’t know here are the main two:
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 on your machine, you need to do a fair bit of preparations installing a few modules to your mac OS;
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 straightforward, 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 in our system, next step is to Install the Java Version Manager.
Installing Java Version Manager - jEnv
As I mentioned above this tool lets you switch between multiple Java JDK versions without 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 it to install the jEnv for us. To do that simply copy and paste the following command to your terminal:
brew install jenvOnce you installed jEnv into your mac, you will see an out as follows:

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 -)"' >> ~/.zshrcSo if you were able to update your shell configuration file successfully your file should look as follows:

Once you are done with the 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 ~/.zshrcYou should now be able to run jenv doctor in the terminal to verify your installation of jEnv.

If you see an output similar to the 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 is 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 mavenAll 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 on 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 javaIf you see an error as follows:

Try this command instead:
brew install --cask javaOnce you installed the java latest version you will see an output as below

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 javaNow you should be able to install Java as follows successfully.

For older Java versions we need to specify the version with Homebrew Cask and 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, use the following commands:
brew install AdoptOpenJDK/openjdk/adoptopenjdk{11,16}# or you can install a single version as follows:brew install AdoptOpenJDK/openjdk/adoptopenjdk8Then you should probably 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 on your machine.
/usr/libexec/java_home -VIt will list all of your installed JDK versions as follows:

Now you can add to jEnv each of the JDKs you installed by the following command:
Syntax: jenv add <your_jdk_path>Example:
jenv add /Library/Java/JavaVirtualMachines/openjdk-14.0.1.jdk/Contents/HomeIf you are successful in the above operation, you will see an output as below:

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 versionsYou can see the input as follows:

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 16.0Setting 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 11Setting 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.252Checking current Java Version
java -versionYou should see an output similar to the below now

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 follow the following articles as well for managing multiple versions of Node and Python in MacOS






