Gangmax Blog

自由之思想,独立之精神

Sdkman: The Usage I Did Not Know

Sdkman is a useful tool for Java developer which I use for quite a long time. Here I list something I just know about it.

1. Add JDK manually

By default you can use the “sdk install java “ command to install the JDK version you want. The JDK list provided by Sdkman is very long, however sometimes the JDK version you want might not be in the list. In such cases you can still add that JDK manually to Sdkman to make it be managed by Sdkman.

1
2
3
4
5
6
7
8
# 1. Download the JDK pacakge file and extract to a directory, such as "sdk-special-version".
# 2. Put the directory into "~/.sdkman/candidates/java".
mv ~/Downloads/sdk-special-version ~/.sdkman/candidates/java
# 3. Now you can see that Sdkman is aware of this new JDK and you can manage it
# like any other JDK versions:
sdk list java
# Note that you have to make sure the path "sdk-special-version/bin/java" must
# be available, otherwise Sdkman cannot work with your JDK properly.

2. Some useful Sdkman commands

From here.

1
2
3
4
5
6
7
8
9
10
# 1. Show the current version numbers of all the working software managed by Sdkman:
sdk c
sdk current
# 2. Show the directory where the given software version is installed:
sdk home gradle 3.5
# 3. Show the broadcast information of Sdkman:
sdk broadcast
# 4. Clean up the local cache of the downloaded package files. This can take up
# a lot of space if you didn't do it before.
sdk flush archives

3. “.sdkmanrc”

In some cases you want to set different software versions for different projects, such as JDK 8 for project A and JDK 11 for project B. It’s doable that using Sdkman command to do the switch every time, but it’s not efficient. For such scenario, “.sdkmanrc” can be used. Get into your project directory and run the following command, which will create a “.sdkmanrc” file in the directory:

1
sdk env init

You can open the file, and set the software versions in it, such as:

1
2
3
4
# Enable auto-env through the sdkman_auto_env config
# Add key=value pairs of SDKs to use below
java=11.0.2-open
gradle=5.6.4

So every time you get in this directory, the software versions will be set as the ones in the “.sdkmanrc” file.

Comments