SDKMAN use any matching java version

Unfortunately, we can't use any matching java version using sdkman. We must provide specific identifier like 11.0.12-open.
Sometimes we need to set just java 11, which is installed, patch number and distribution don't matter.
There is open issue on github #920 with proposal of this feature.

SDKMAN autocomplete make it easier to set java version without knowing specific identifier. After you enter in console sdk use java 11 and click TAB twice, it shows list of installed java distributions.

Problem is when we need to set java version in some script (for example in Continuous Integration).
Therefore, below is a script that can be used to accomplish such a task.

Save script as sdk_use_any_java.sh

1#!/bin/bash
2java_sdkman=$(find ~/.sdkman/candidates/java -mindepth 1 -maxdepth 1 -type d -printf "%f\n" | grep "^$1" | sort -r | head -n1)
3if [[ -z $java_sdkman ]]; then
4  echo "Can't find java $1 installed by sdkman"
5  exit
6fi
7source "$HOME/.sdkman/bin/sdkman-init.sh"
8sdk use java $java_sdkman

Using script to set java 11

1./sdk_use_any_java.sh 11

I also wrote a script to add the sdkj alias so you can call the sdkj 11 command to set java 11.

1alias sdkj='_sdkman_use_any_java(){ java_sdkman=$(find ~/.sdkman/candidates/java -mindepth 1 -maxdepth 1 -type d -printf "%f\n" | grep "^$1" | sort -r | head -n1) && if [[ -z $java_sdkman ]]; then echo "Cant find java $1 installed by sdkman"; return; fi && source "$HOME/.sdkman/bin/sdkman-init.sh" && sdk use java $java_sdkman; }; _sdkman_use_any_java'

Below script to install alias. It is available on GIST:

1curl https://gist.githubusercontent.com/speedlog/c96ed2c40588c0482ea7123a5ae2f241/raw/c625bf9025634b4ee94336f8d1bd89276c9b68b0/sdk_use_any_java.sh >> ~/.bashrc
2source ~/.bashrc

Translations: