Probably many of us have to work with multiple versions of Java SDK. We need to install three, four or even more of them locally. In order to help with that the SDKMAN tool was developed. It’s a great utility for managing different versions of SDKs (not Java only) but I found one particularly useful feature missing.

SDKMAN

Using SDKMAN you can easily list your Java installations and install a new one:

$ sdk list java
================================================================================
Available Java Versions
================================================================================
 Vendor        | Use | Version      | Dist    | Status     | Identifier
--------------------------------------------------------------------------------
 AdoptOpenJDK  |     | 13.0.2.j9    | adpt    |            | 13.0.2.j9-adpt
               |     | 13.0.2.hs    | adpt    | installed  | 13.0.2.hs-adpt
               |     | 12.0.2.j9    | adpt    |            | 12.0.2.j9-adpt
               |     | 12.0.2.hs    | adpt    |            | 12.0.2.hs-adpt
               |     | 11.0.6.j9    | adpt    |            | 11.0.6.j9-adpt
               | >>> | 11.0.6.hs    | adpt    | installed  | 11.0.6.hs-adpt
               |     | 11.0.4.hs    | adpt    | local only | 11.0.4.hs-adpt
               |     | 11.0.3.hs    | adpt    | local only | 11.0.3.hs-adpt
               |     | 8.0.242.j9   | adpt    |            | 8.0.242.j9-adpt
               |     | 8.0.242.hs   | adpt    | installed  | 8.0.242.hs-adpt
               |     | 8.0.212.hs   | adpt    | local only | 8.0.212.hs-adpt
 Amazon        |     | 11.0.6       | amzn    |            | 11.0.6-amzn
...
$ sdk install java 13.0.2.hs-adpt

Problem

The problem is that it has a complex identifier (e.g. 13.0.2.hs-adpt) to remember when switching between Java versions. Usually I have the following options:

  1. Run sdk list java, find the installed versions (those with non-empty status), then copy the identifier and use it in the next command: sdk use 13.0.2.hs-adpt.
  2. If I used the given Java version before I just grep my shell history (Ctrl+R) for sdk use java and re-use the command.
  3. install autocomplete plugin for the shell but still need to type sdk u<TAB> j<TAB> and then choose the version I want to use.

None of them is very user-friendly.

Usually I don’t care about the minor version of Java I want to use. I just want the latest 13 or 11 I have on my machine.

Why not make it simpler by specifying a major version only?

Solution

That’s why I’ve created the j script, very simple yet quite useful and time-saving. It checks for the local Java installations and picks up the latest version of the chosen major Java version:

$ j
Available versions:
13
11
8
Current: 11
Usage: j <java_version>

$ j 8
Using java version 8.0.242.hs-adpt in this shell.

No need to remember the exact identifier nor search the history. Just type j 13 and use your latest local Java 13.

More info: https://github.com/ldziedziul/j