Ukraine needs your help!

Support

How to manage JDK version with SDKMAN

Tools, Java

Usually, I was working with different projects using different SDK versions that required switching between versions. If you still did not face SDKMan tool welcome to this post.

SDKMan - is a tool that allows to manage of different versions of SDK, Maven, Gradle etc.

The tools was developed inspired by the very useful tool called RVM and rbevn used by Ruby community.

Install

First of all, we should install SDKMan locally.

If you are using Windows, you have to use SDKMan through WSL2. If you don’t have it follow this guide.

Open the Terminal and execute installation command:

curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"

Done! Now, let’s try to execute the following command:

> sdk version

SDKMAN 5.15.0

Installing a JDK

Let’s imagine that I want to install Java, for that, I have to take a look at the list of available versions:

> sdk list java

==============================================================================
Available Java Versions for Linux 64bit
==============================================================================
 Vendor        | Use | Version      | Dist    | Status     | Identifier
------------------------------------------------------------------------------
 Corretto      |     | 18.0.1       | amzn    |            | 18.0.1-amzn         
               |     | 17.0.3.6.1   | amzn    |            | 17.0.3.6.1-amzn     
               |     | 17.0.0.35.1  | amzn    |            | 17.0.0.35.1-amzn    
               |     | 11.0.15.9.1  | amzn    |            | 11.0.15.9.1-amzn    
               |     | 11.0.12.7.1  | amzn    |            | 11.0.12.7.1-amzn    
               |     | 8.332.08.1   | amzn    |            | 8.332.08.1-amzn     
 Dragonwell    |     | 11.0.12.8    | albba   |            | 11.0.12.8-albba     
               |     | 8.8.9        | albba   |            | 8.8.9-albba         
 GraalVM       |     | 22.1.0.r17   | grl     |            | 22.1.0.r17-grl      
               |     | 22.1.0.r11   | grl     |            | 22.1.0.r11-grl

Let’s choose the version which I needed in Identifier column and execute the installation command:

sdk install java 17.0.3.6.1-amzn

By default, installed SKDs will be located in ~/.sdkman/candidates/java/ folder:

> ls -al ~/.sdkman/candidates/java/

drwxr-xr-x 10 axbarchuk axbarchuk 4096 Jan  8 15:14 17.0.2-zulu
drwxr-xr-x  9 axbarchuk axbarchuk 4096 Apr 16 01:01 17.0.3.6.1-amzn
lrwxrwxrwx  1 axbarchuk axbarchuk   15 Jun 15 13:05 current -> 17.0.3.6.1-amzn

Switching between SDK versions

As you can see above we can install any count of versions which we need.

Executing the next command we could easily switch the current version to needed for a particular project, for instance.

sdk use java 11.0.9.j9-adpt

If you want to set a specific version as default, use this command:

sdk default java 17.0.2-zulu

Managing SDKs in projects

Let’s imagine that we are working with a few projects, for instance, we are developing microservices where some of them use java 11 and some of them already moved to Java 17.

How to understand for what service which version of SDK is used.

For that purposes SDKMan provides a file called as .sdkmanrc were we could describe the versions of SDKs which our project are used.

First of all, you have to execute SDKMan init:

sdk env init

As a result will be created .sdkmanrc file, next you have to describe the versions of SDKs which your project will use:

java=17.0.2-zulu

If your project already have this file, you just need to use it by executing following command:

> sdk env

Using java version 17.0.2-zulu in this shell.

This command automatically sees the file with that name and use it.

Tools

Except for SDKs you could install needed tools such as:

  • Maven
  • Gradle
  • Spring Boot CLI
sdk install maven
sdk install gradle
sdk install springboot

Also, if you notice above I don’t use the versions of installing tools, in that case, will be using the latest stable versions of that package. The same is applicable to SDKs.

Conclusion

SDKMAN! is a great tool to manage the versions of SDKs or our favorite tools. It allows managing SDK without headache. If you need more information visit the official site.

Comments

Alex BarchukBlog about programming 🚀bcode.dev © 2022