Are you a Java developer, looking for a hassle-free way to use multiple versions of Java, without ever having to interact with the 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.

Yes. You read it correctly. With the command-line interface provided by SDKMAN!, you can install multiple versions of your favourite SDKs (not just JDK) on your favourite *nix based system!

Installation

Installing it is as easy as executing below command in the terminal:

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

Confirm the installation by checking out the installed version. If you get command not found, exit and start a new terminal session.

$ sdk version

Default installation directory

By default, the CLI is installed under $HOME/.sdkman/ directory.

If you want to use a custom directory for the installation, let’s say /Desktop/sdkman/, then execute the following command:

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

For the above command to succeed, make sure that the folder sdkman does not exist.

Packages

As mentioned before, the sdk command allows us to install various development tools. ‘Which SDK tools are available?’ you may ask. Following command tells you exactly that:

$ sdk list

While various JVM based languages including Java, Groovy, Kotlin and, Scala are there; don’t get surprised if you see one of your favourite frameworks (Spring Boot, Vert.x or Micronaut) among the list! Some of the popular build tools (Ant, Gradle and Maven) are also included.

Now then, back to our main objective…

Installing Java

Previous command gave us a list of all available SDKs, and we know that most of them have multiple versions available. What if we want to know exactly which versions of a particular language or framework or build tool are available?

That is as easy as passing the name of that SDK:

$ sdk list java

You’ll see a list of Java versions available for install through the sdk command.

To install a particular version, pass in that version signature to the following command (replacing {javaVersion}):

$ sdk install java {javaVersion}

Now sit back and relax. The Java package will get downloaded, installed and the JAVA_HOME will be set to this particular 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 and install it with sdk install java {java7Version}. This is where things get interesting.

Since we already had a Java version installed earlier, SDKMAN! will take a note of that and after installation, will ask you whether you want this version to be set as default or not. If you say ‘yes’, then the default version will be set to newly installed one (Java 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 answer for that!

Know that there are two approaches for switching versions:

  1. For current terminal session only

    $ sdk use java {javaVersion}
    
  2. Switch globally

    $ sdk default java {javaVersion}
    

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

Uninstall

When your favourite SDK becomes outdated or is not longer used, we have to get rid of it, to give that HDD of ours some space to stretch into. You can do that with ease using sdk:

$ sdk uninstall <sdk> {sdkVersion}

You can follow the same approach to install any other SDK. Just learn 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 the same. It is pretty good at cleanup too.