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

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
7source "$HOME/.sdkman/bin/"
8sdk use java $java_sdkman

Using script to set java 11

1./ 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/" && sdk use java $java_sdkman; }; _sdkman_use_any_java'

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

1curl >> ~/.bashrc
2source ~/.bashrc