Hello again. In anticipation of the start of the course, Java Developer prepared a translation of an interesting article for you.

ITKarma picture



If you, like me, have to work simultaneously with several projects (both Legacy and modern), and perhaps you are developing some kind of library or utility for users with different versions of Java, then you will understand the problem of switching between different JDK.

ITKarma picture

Nowadays, installing, managing and switching between JDKs during development becomes a daunting task because a large number of developers still work with Java 8, although many are already migrating to Java 11. This problem can be solved in many ways. In this article, we’ll cover some of them.

Manual mode


You can download the distributions from the JDK vendor’s site and install them all manually (or just unzip them to some folder like CDMY0CDMY) and that’s it. But then you have to check and update CDMY1CDMY every time so that the JDK you need is there.

This can be done using bash scripts, bash functions, etc. What is the problem then? You can simply forget to call the script/function and realize that something went wrong only when you see CDMY2CDMY.

If you prefer to do everything manually, you can dig through the Internet to find solutions. And this can be a good option for advanced users. But I use a couple of utilities for this. So let's take a look at them.

The best way


In my opinion, the ideal solution would be to combine two open source tools that have existed for quite some time, and perhaps you are familiar with at least one of them. But what you don’t know is that they can be used together.


Let's look at their sharing.

Installing multiple JDKs


ITKarma picture
After installing SDKMAN! , type:

$ sdk list java 

Find the vendor you need, version and enter:

$ sdk install java <id> 

Let's install four JDKs: the last two versions of Oracle OpenJDK and the two LTS versions of AdoptOpenJDK.

When installing , do not select the default JDK.

  1. AdoptOpenJDK assembly OpenJDK 8u252 - LTS
  2. AdoptOpenJDK assembly OpenJDK 11.0.7 - LTS
  3. Oracle OpenJDK 14 - Latest GA
  4. Oracle OpenJDK 15 - Early Access build

These four versions should meet your needs for legacy Java 8 projects, for modern Java 11 projects, and for experimenting with innovations in Java 14 and Java 15.

Fine. You have four JDKs installed locally using the SDKMAN! I think this utility is really convenient for installing JDK and I hope you enjoyed it too.

All installed JDKs can be found in the following directory:

$ cd/Users/bruno/.sdkman/candidates/java $ ls 11.0.7.hs-adpt 14.0.1-open 15.ea.20-open 8.0.252.hs-adpt 

If you don’t need to switch between different JDKs often, this will be enough. You can use SDKMAN! to select JDK:

$ sdk current java Using java version 15.ea.19-open $ sdk default java 15.ea.20-open Default java version set to 15.ea.20-open 

You can stop at this and use only SDKMAN !, but sooner or later, you will forget to switch the JDK in the same way as with the manual approach. Therefore, I prefer JDK auto-switching.

Managing multiple JDKs with jEnv


ITKarma picture

Despite the fact that SDKMAN! It’s convenient to install the JDK, but it does not help when switching automatically between different JDKs when moving from one project to another. jEnv helps here.

After you install several JDKs using SDKMAN! or in any other ways, you will need to add them to jEnv:

$ cd ~/.sdkman/candidates/java $ ls -1 11.0.7.hs-adpt 14.0.1-open 15.ea.20-open 8.0.252.hs-adpt $ jenv add 15.ea.20-open openjdk64-15-ea added 15-ea added 15-ea added... $ jenv versions system 1.8 1.8.0.252 11.0 11.0.7 14.0 14.0.1 * 15-ea (set by/Users/bruno/.jenv/version) openjdk64-1.8.0.252 openjdk64-11.0.7 openjdk64-14.0.1 openjdk64-15-ea 

Repeat the CDMY3CDMY command for the remaining three versions of the JDK.

jEnv is especially convenient for those who constantly use the command line. He will let you:

  1. Install the Java version globally for your system.
  2. Install the Java version for the current directory/project you are in.
  3. Install the Java version for the current shell.

jEnv uses shim and automatically manages the JAVA_HOME environment variable. Thus, after you configure your system, jEnv will switch the JDK version to the most suitable one, based on the above priorities and the current directory. Cool! It also has other useful features - see the website for more information.

Automatically switch between JDK


You now have Java 8, 11, 14, and 15-EA installed. So how do you switch between them? Let's configure the versions and test the switch.

First you need to specify the default global JDK in the system. To do this, I usually use Early Access. Whenever I start working with a new project, I automatically use the upcoming release, which helps me identify potential problems that I can report to the OpenJDK project. You do that too...

$ jenv global 15 

Now for any folder you are in, when you run CDMY4CDMY you will get OpenJDK 15 EA.

If you have a project that should use Java 8, then go to the folder and run:

$ jenv local 1.8 


This will create a file called CDMY5CDMY with the following contents:

$ cat.java-version 1.8 

This file tells jEnv which JDK to use when you are in this folder.

Finally, if you want to temporarily switch to another version of the JDK, then use CDMY6CDMY.

There is a demo here that can explain all of the above and how auto-switching works.

After the article was published on Reddit , there were some interesting comments worth mentioning here:

  • SDKMAN! since version 5.8.1+ it supports sdk env and automatic switching is also available there.
  • direnv is very good and not tied to java. It does the same as jEnv, except it doesn't help you switch JDK easily. You must edit the files manually.
  • autoenv is another direnv alternative with the same limitations.
  • jabba is the most promising alternative to combining SDKMAN + jEnv, because it supports all their functionality and works in Windows (PowerShell), which is not supported by others, the tools mentioned above.

I hope you enjoyed reading. If you have any questions, just contact me on Twitter .



MANAGE VERSIONS OF THE DATABASE THROUGH FLYWAY



More on the topic


.

Source