Java on LinuxNovember 18, 2018
  • java
  • sdkman

Are you a Java developer looking for a hassle-free way to use multiple versions of Java without ever having to interact with JAVA_HOME variable? Well, look no further!

Introducing SDKMAN!

From the official site:

SDKMAN! is a tool for managing multiple versions of different Software Development Kits on most Unix based systems.

With its command-line interface you can install multiple versions of your favourite SDKs (not just JDK!) on your favourite *nix based system. For now, let's focus on managing multiple JDKs, shall we?

Installation

Installing it is as easy as executing below command from your terminal:

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

Restart your terminal session for the installation to take affect, otherwise you might receive command not found: sdk error.

Now you can check the installed version to verify your installation.

$ sdk version

Default installation directory

The CLI is installed under your $HOME/.sdkman/ directory by default.

If you want to use a custom directory instead, say /Desktop/sdkman/, then execute the following command:

Make sure that folder sdkman does not exist.

$ export SDKMAN_DIR="/Desktop/sdkman" && curl -s "https://get.sdkman.io" | bash

Packages

As we saw earlier that sdk command allows us to install various development tools. Following command shows us a list of those tools:

$ sdk list

While various JVM based languages including Java, Groovy, Kotlin and, Scala are there; don't be surprised if you see one of your favourite frameworks like Spring Boot, Micronaut and even Quarkus among the list! Popular build tools Ant, Gradle and Maven are also included.

Now then, back to our main objective...

Installing Java

Now that we know which languages, frameworks and build tools are provided by sdk, what if we want to know all the available versions of a package?

Doing that is as easy as passing the name of that package:

$ sdk list java

You'll get a list of Java versions available for install. To install a specific version, pass in that version signature as follows, replacing {version}:

$ sdk install java {version}

Now sit back and relax. The Java package will get downloaded, installed and the JAVA_HOME will be set to specified version. Automatically!

Multiple Versions

Assume that for some reason you have to use Java 7. Let's use sdk to install the same.

Check the version signature of Java 7 using sdk list java, then install it with sdk install java {version-7}. This is where things get interesting.

Since we already had a Java version installed earlier, SDKMAN! will take a note of that and ask you whether you want this new version to be set as default. If you say 'yes', then the default version will be set to newly installed version, JDK 7! Pretty cool, isn't it?

Let's verify the current version of Java installation:

$ sdk current java

You'll see version 7 as your currently used one.

Switch Versions

Since we have two different versions of Java installed, what if we want to switch between them? SDKMAN! also has an answer to that!

Know that there are two approaches for switching versions:

  1. For current terminal session only
$ sdk use java {version}
  1. Switch globally
$ sdk default java {version}

You can verify the version changes using java -version and javac -version command.

Uninstall

When your favourite SDK becomes outdated or is no longer used, you have to get rid of it to save some space. You can do that with ease:

$ sdk uninstall <sdk> {version}

You can follow the same approach to install any other SDK. Identify which version you want and install it with sdk. And when the time comes to use different versions of your favourite SDK, you know what to do.

Conculusion

SDKMAN! is an interesting command-line client. It not only provides a way to easily install one of the many provided SDKs, but also makes it easier to use and switch between different versions of them. It is pretty good at cleanup too.