Install/Manage Multiple JAVA versions — MacOS BigSur

Lakshan Thilakarathne
3 min readFeb 16, 2022

Greetings geeks 👋 …

Normally we need to switch multiple java versions when we are switching/troubleshooting different projects right?

So, Today we are going to discuss on how to do this in a hassle-free way. 😎

jenv

We are using the jenv command-line tool for this.
We can install it by cloning the repo or using our most popular package manager Homebrew. Let’s move with homebrew.

If you don’t have homebrew, please run the below command to install as in https://brew.sh/

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

Then we can install the jenv as below,

brew install jenv

Next, we need to add the below to the shell configuration file as per the above output. In my case, it’s ~/.zshrc. If you are using bash, then it would be ~/.bash_profile or ~/.bashrc.

Then we need to restart the terminal or re-source your configurations as below.

source ~/.zshrc

To verify jenv was installed, run jenv doctor.
You’ll observe the following output (Since we just installed the jenv, it shows that the jenv is correctly loaded but java is not yet installed.):

Run the below command as well in order to make sure that the JAVA_HOME is set.
jenv enable-plugin export

Restart the terminal and echo the JAVA_HOME.

👏 Great Job! We have successfully installed the jenv.

Install Multiple Java Versions

Here is the interesting part where we going to install the JDKs.

1. Install the latest OpenJDK

Run the below command to install using homebrew.

brew install java

Above will install the latest(17.0.2) OpenJDK version as per the now.

2. Adding the JDK home to jenv

Then we need to notify jenv about the installed JDK in order to perform the switching.

jenv add <JDK_PATH>

You will see the above output if it is successfully added.

Let’s install another JDK version which is 11.

Then we can list all the added JDKs to the jenv using the below command.

jenv versions

Setting a Global Java Version

Use the below command to set the global java version,

jenv global <JAVA_VERSION>

Setting a Local Java Version for the current working directory

Use the below command to set the local java version,

jenv local <JAVA_VERSION>

Setting a shell instance Java Version

jenv shell <JAVA_VERSION>

That’s it and now you can switch between multiple java versions in a few keystrokes.

I hope that you find this article helpful…

Happy Coding!
CHEERS! ✌️

--

--