In this blog, you will learn how to manage several Java Development Kits (JDKs) on your Linux system using SDKMAN. Besides JDKs, several other SDKs can be managed by means of SDKMAN. Enjoy!

1. Introduction

Some time ago, a colleague of mine showed me how he used SDKMAN and how easy he could maintain different versions of JDKs, Maven, etc. on his Linux system. I had heard of SDKMAN before but did not yet take the time to take a closer look at it. But now I did :-).

Before getting started with SDKMAN, I looked at which JDKs were present on my Linux system (using Ubuntu 20.04.4 LTS version).

First, I looked at what update-alternatives returns for Java. It seems that I have two java 11 versions on my system.

$ update-alternatives --list java
/usr/lib/jvm/java-11-openjdk-amd64/bin/java
/usr/lib/jvm/jdk-11.0.3+7/bin/java

But, I also have installed JDKs via IntelliJ. These can be found in your home directory in the .jdks directory. This reveals 3 other versions of Java.

$ ls .jdks/
openjdk-14.0.1                               openjdk-16.0.2
openjdk-14.0.1-intellij-downloader-info.txt  openjdk-17

My conclusion is: I made a mess out of it. Let’s see whether SDKMAN can help me with that and afterwards I will clean up my system and will remove all the redundant JDKs.

2. Installation

Installation of SDKMAN is quite straightforward. Just execute the following command.

$ curl -s "https://get.sdkman.io" | bash
...
All done!


Please open a new terminal, or run the following in the existing one:

    source "/home/<your user>/.sdkman/bin/sdkman-init.sh"

Then issue the following command:

    sdk help

Enjoy!!!

And source the init file as requested.

$ source "/home/<your user>/.sdkman/bin/sdkman-init.sh"

You can verify the installation by requesting the version of SDKMAN.

$ sdk version 
==== BROADCAST =================================================================
* 2022-03-03: jbang 0.90.2 available on SDKMAN! https://github.com/jbangdev/jbang/releases/tag/v0.90.2
* 2022-03-02: quarkus 2.7.3.Final available on SDKMAN! https://github.com/quarkusio/quarkus/releases/tag/2.7.3.Final
* 2022-02-28: micronaut 3.3.4 available on SDKMAN!
================================================================================

SDKMAN 5.13.2

As you can see, the latest releases are displayed and this can be quite helpful to get notified of new available versions.

That’s it for the installation!

3. List JDKs

In order to know which tools can be installed with SDKMAN you can take a look at the website or you can issue the list command. The output is redirected to less where you can use the basic commands to browse and search the list.

$ sdk list

A more specific list for available versions of the JDK can be obtained by means of the list command following java. The list of available JDKs is also available at the website. Again, the output is redirected to less.

$ sdk list java

4. Install JDK

First, I am going to verify which version of Java is the default on my system. It seems that I am using AdoptOpenJDK version 11.0.3 which is older than I expected.

$ java --version
openjdk 11.0.3 2019-04-16
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.3+7)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.3+7, mixed mode)

From SDKMAN, choose the AdoptOpenJDK JDK which is now provided by Adoptium and is called Eclipse Temurin. Install the latest JDK 17 LTS version.

$ sdk install java 17.0.2-tem 

Downloading: java 17.0.2-tem

In progress...

...

Repackaging Java 17.0.2-tem...

Done repackaging...

Installing: java 17.0.2-tem
Done installing!


Setting java 17.0.2-tem as default.

Verify again what the default Java version is. It has changed to the one you just installed.

$ java --version
openjdk 17.0.2 2022-01-18
OpenJDK Runtime Environment Temurin-17.0.2+8 (build 17.0.2+8)
OpenJDK 64-Bit Server VM Temurin-17.0.2+8 (build 17.0.2+8, mixed mode, sharing)

Also install the JDK 11 LTS version in a similar way. At the end, the question is asked whether you want this JDK to be the default. Answer with no, JDK 17 is the latest LTS version of Java and should be ok to be the default.

$ sdk install java 11.0.14-tem 

Downloading: java 11.0.14-tem

In progress...

...

Repackaging Java 11.0.14-tem...

Done repackaging...

Installing: java 11.0.14-tem
Done installing!

Do you want java 11.0.14-tem to be set as default? (Y/n): n

5. Default JDK

If you change your mind, and you still want JDK 11 to be the default, then you can do so by means of the default command.

$ sdk default java 11.0.14-tem 

Default java version set to 11.0.14-tem

Verify this.

$ java --version
openjdk 11.0.14 2022-01-18
OpenJDK Runtime Environment Temurin-11.0.14+9 (build 11.0.14+9)
OpenJDK 64-Bit Server VM Temurin-11.0.14+9 (build 11.0.14+9, mixed mode)

And set it back again to JDK 17.

$ sdk default java 17.0.2-tem 

Default java version set to 17.0.2-tem

It is also possible to change the default JDK in your terminal session only, by means of the use command.

$ sdk use java 11.0.14-tem 

Using java version 11.0.14-tem in this shell.

In this terminal window, the JDK is now JDK 11.

$ java --version
openjdk 11.0.14 2022-01-18
OpenJDK Runtime Environment Temurin-11.0.14+9 (build 11.0.14+9)
OpenJDK 64-Bit Server VM Temurin-11.0.14+9 (build 11.0.14+9, mixed mode)

Open another terminal window and there the JDK is still JDK 17. Note that also the JAVA_HOME environment variable points to the correct Java version.

$ echo $JAVA_HOME
/home/<your user>/.sdkman/candidates/java/current

6. Remove Other JDKs

Now that the JDKs are installed via SDKMAN, it is time to remove all the other JDKs which are on my system.

$ dpkg --list | grep jdk
ii  openjdk-11-jre-headless:amd64              11.0.13+8-0ubuntu1~20.04                amd64        OpenJDK Java runtime, using Hotspot JIT (headless)

Remove the openjdk-11-jre-headless:amd64 version.

$ sudo apt remove openjdk-11-jre-headless:amd64

Verify the update-alternatives again and note that still a Java version is available.

$ update-alternatives --list java
/usr/lib/jvm/jdk-11.0.3+7/bin/java

Remove it from update-alternatives.

$ sudo update-alternatives --remove "java" /usr/lib/jvm/jdk-11.0.3+7/bin/java
update-alternatives: removing manually selected alternative - switching java to auto mode

Verify the update-alternatives. It is removed now.

$ update-alternatives --list java
update-alternatives: error: no alternatives for java

However, it is not yet removed from the file system. Navigate to the directory /usr/lib/jvm and remove the JDK.

$ sudo rm -rf jdk-11.0.3+7/

What is left to do, is to remove the JDKs installed by IntelliJ. These are located in your home directory in directory .jdks. Navigate to your home directory and remove the .jdks directory.

$ rm -rf .jdks/

7. Use JDKs in IntelliJ

Open IntelliJ and navigate to File – Project Structure. This is the location where the JDKs need to be configured. It is a bit strange that this is located in Project Structure and not in the general Settings dialog. By means of the + sign, you can add the SDKMAN JDKs.

You also need to choose the JDK for your project.

Build you project and in the log you will notice that the SDKMAN JDK is being used.

8. Conclusion

SDKMAN is an easy to use tool to control different versions of e.g. the JDKs you use. It is definitely something for your development toolkit. I am very pleased with it. It is less a mess on my system and it is easy for me to control the different versions on my system. In order to check whether there are new updates, the upgrade command can be used.