Header image

Setup jEnv to manage multiple Java environments

Øyvind Ahlstrøm
4 min readJan 26, 2020

--

You are working in an organization and maintain several Java based projects. Your team uses the newest version of Java for the new web applications that take advantage of all the new and cool features. Some of the earlier projects are lagging behind and are due for some maintenance. One day the project owner approaches your team and asks you to maintain a handful of the older projects. This involves updating business logic and adding new features. You are not known to shy away from a challenge so you accept the assignment.

While getting familiarized with the older projects you notice something. The Java version installed on your developer machine is different from the one in the older projects. You learn that these applications run on an older infrastructure and for the time being must continue to do so. Your team does not look forward to having to work with several different Java environments and all the challenges it entails. There is, however, a great solution for these situations.

Say hello to jEnv

jEnv is a command line tool that assists you in switching between installed versions of Java. It sets your JAVA_HOME variable in your active shell. You can set Java version globally or locally. This tool can be great for your team. The following text will provide a step by step tutorial to set up both Java and jEnv for MacOS.

Download jEnv and Java

Open a terminal and install jEnv through Homebrew with the command:

brew install jenv

After jEnv has successfully downloaded you need to update the configuration file for your terminal. This file is placed on root and is named .bash_profile. If you use another shell, you would need to find the correct file name. If you use zsh (z shell), then the configuration file is named .zshrc. It should be located in the same place. When you have located the correct file, open it and add the following two lines:

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

Great, you have successfully installed jEnv. The next step is installing Java. We will continue to use Homebrew.

Homebrew offers AdoptOpenJDKs versions of Java. First, tap the barrel containing all the AdoptOpenJDK versions. Tapping a barrel means that your Homebrew will add the repositories to available software. Run the following command in the terminal:

brew tap AdoptOpenJDK/openjdk

Once you have tapped the openjdk barrel you can install your desired Java version. See information about available versions here: Homebrew/AdoptOpenJDK.

To install Java 11, run the following command in the terminal:

brew cask install adoptopenjdk11

You can attempt to download a few different versions. Once you have done that, you can see a list over installed versions with the following command:

/usr/libexec/java_home -V
A screenshot of the response from the command that lists installed Java versions.
List of installed Java versions

Have this command close by. It comes in handy when you want to add or remove versions in jEnv.

Let jEnv know what versions are available

Your team decides to have a few different versions of Java installed. The previous command presented you with a response that provides information about your installed Java versions. This includes the path to where the version is installed.

Your team needs to let jEnv know which Java version it should have available. To do that you need to tell it where the version is installed. Let’s add Java 11 to jEnv by running the following command:

jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home
Screenshot of the response from jenv after running the jenv add command.
The response from jEnv after running the jenv add AdoptOpenJDK 11

Do this for all the versions you want to have available in jEnv. If you get an error while running this command, open a new tab in the terminal (cmd + t) or restart the terminal completely.

Organize your jEnv references

To see a list of all the versions you have made available for jEnv you can run the following command:

jenv versions

This will list all the Java versions jEnv has indexed. You might see that there are duplicates for each Java version. For example ‘openjdk64–11.0.6’, ‘11.0.6’ and ‘11.0’. These all point to the same version of Java 11, however they provide different granularity of information in their name. You decide that the extra information is not necessary and you only want to operate with the major version. You therefore remove the other references to declutter the list by executing the following command:

jenv remove openjdk64-11.0.6
jenv remove 11.0.6

Don’t worry. This will not delete anything. It only removes the reference from jEnv and will result in making the list of installed versions easier to read.

Don’t forget Maven

Maven is often used as a tool with Java. To integrate Maven with jEnv write the following command:

jenv enable-plugin maven
jenv enable plugin export

Set active Java version

The only thing remaining for your team is to set an active Java version. You can set a global version or a local version. Start by setting a global version:

jenv global 11.0

Verify that AdoptOpenJDK version 11 is now the active Java version by executing the following command:

java -version

To set a local version (per directory):

jenv local 12.0

To set a version for the shell instance:

jenv shell 13

Sources and inspiration

--

--