As Java developers we often must work with multiple projects at the same time. Even if all projects you develop as a part of your job use the same JDK version, you might still have private projects with a different one, or you simply want to experiment with the new JDK as it’s released. In those cases, using jEnv to assign an appropriate JDK to every project is a life saver. Best of all, it’s very easy to use and it doesn’t require wrangling with the JAVA_HOME environment variable. Let’s quickly see it in action!

Installation

If you don’t already have jEnv you must install it. Since this heavily depends on your OS and shell, I will just refer you to the official documentation where you’ll find instructions for various combinations of OS and shell. If you use Windows, you’ll need either GitBash or Windows Subsystem for Linux. With that out of the way, let’s move to adding a JDK to the jEnv.

Adding a JDK

I’ll assume you already downloaded one or more JDKs you want to use. If not, you can follow my article on How to install JDK 18 on MacOS. With jEnv, you don’t have to actually install JDKs (in a way you install other applications), it’s enough to unpack them and they’re ready to be used by jEnv! Adding a JDK is super simple. This example shows adding JDK 17 and JDK 18:

 1# ivanmilosavljevic @ ARTEMIS in ~ [13:52:47]
 2$ jenv add /Users/ivanmilosavljevic/jdk/jdk-17.0.1.jdk/Contents/Home/
 3openjdk64-17.0.1 added
 417.0.1 added
 517.0 added
 617 added
 7
 8# ivanmilosavljevic @ ARTEMIS in ~ [13:53:33]
 9$ jenv add /Users/ivanmilosavljevic/jdk/jdk-18.jdk/Contents/Home/
10openjdk64-18-ea added
1118-ea added

Listing available JDKs

If you want to see all JDKs which are known by jEnv just type jenv versions:

 1# ivanmilosavljevic @ ARTEMIS in ~ [13:53:41]
 2$ jenv versions
 3  system
 4  1.8
 5  1.8.0.232
 6  11.0
 7* 11.0.5 (set by /Users/ivanmilosavljevic/.jenv/version)
 8  17
 9  17.0
10  17.0.1
11  18-ea
12  openjdk64-1.8.0.232
13  openjdk64-11.0.5
14  openjdk64-17.0.1
15  openjdk64-18-ea

You can see that some of them are aliased (e.g. 17, 17.0, 17.0.1, openjdk64-17.0.1) which means you can use any of these names to refer to the same JDK. Also, one of them is the default global one and it’s marked with asterisk. But how can we select such a global JDK and separate JDKs for various projects?

Managing JDK versions

With jEnv we can select one global system-wide JDK and a bunch of local ones for specific projects. Respective commands are jenv global and jenv local.

To select JDK 17 as a global one, simply type jenv global 17:

1# ivanmilosavljevic @ ARTEMIS in ~ [13:59:08]
2$ jenv global 17
3
4# ivanmilosavljevic @ ARTEMIS in ~ [13:59:12]
5$ java -version
6openjdk version "17.0.1" 2021-10-19
7OpenJDK Runtime Environment (build 17.0.1+12-39)
8OpenJDK 64-Bit Server VM (build 17.0.1+12-39, mixed mode, sharing)

To select a local JDK for a directory, simply go to that directory and type jenv local <version>:

 1# ivanmilosavljevic @ ARTEMIS in ~ [14:11:13]
 2$ cd src/java18-school
 3
 4# ivanmilosavljevic @ ARTEMIS in ~/src/java18-school [14:11:18]
 5$ jenv local 18-ea
 6
 7# ivanmilosavljevic @ ARTEMIS in ~/src/java18-school [14:11:21]
 8$ java -version
 9openjdk version "18-ea" 2022-03-22
10OpenJDK Runtime Environment (build 18-ea+28-1975)
11OpenJDK 64-Bit Server VM (build 18-ea+28-1975, mixed mode, sharing)

Setting JAVA_HOME environment variable

Even when you set up global JDK with jEnv, some applications from the Java ecosystem will search for JAVA_HOME environment variable in your system to find out where the JDK is located. Also, if you want to create and use executable Java scripts as single-file source code in GNU/Linux or MacOS then you must set JAVA_HOME. Luckily with jEnv it is super easy - just run jenv enable-plugin export and restart your terminal. You only have to do this when you change global JDK.

Exploring jEnv further

I’ve covered the most important use cases of jEnv. But there’s always more to explore and learn so I encourage you to run jenv commands to see which commands exist in jEnv and then jenv help command for more details on each command you want to explore. Be sure to check at least jenv help doctor, jenv help global and jenv help local.

It’s time for you to take action!

The best way to extract the most out of jEnv is to install it today. Then add to it all the JDKs you already have on your machine, or download the newest ones and add them to jEnv. Finally, start a project or two using different local JDKs controlled by jEnv. In no time, you will be a master of jEnv and be able to switch at will to whatever JDK you need thus saving some time and nerves!

Dear fellow developer, thank you for reading this article about managing multiple JDKs with jEnv. Until next time, TheJavaGuy saluts you!