+# Sets up a new Java JDK
+# Loosely based on https://askubuntu.com/questions/1079239/can-not-install-jdk11-using-update-alternatives-install
+ echo "Usage: $(basename $0) [-h] JDK [PRIORITY [ALIAS]]
+ Configures a new Java JDK as downloaded from https://jdk.java.net and
+ untarred into /usr/lib/jvm.
+ -h prints this help and exits
+ JDK the OpenJDK directory in /usr/lib/jvm/\$JDK
+ PRIORITY an optional priority value (numeric), default: 1010
+ ALIAS an optional JDK alias
+ Allows supporting multiple JDKs on the same Ubuntu installation, and the use
+ of update-java-alternatives to switch between them.
+ This script expects the OpenJDK root folder to be in /usr/lib/jvm and its name
+ to match the first argument (JDK); it will also create .\$JDK.jinfo file in the
+ /usr/lib/jvm folder (used by the update-java-alternatives command).
+ See also: http://manpages.ubuntu.com/manpages/precise/man8/update-java-alternatives.8.html
+if [[ ${1:-} == "-h" ]]; then
+jinfo="/usr/lib/jvm/.${jdk}.jinfo"
+if [[ -z ${jdk} || ! -d /usr/lib/jvm/${jdk} ]]; then
+for c in $(ls /usr/lib/jvm/${jdk}/bin); do
+ bin=/usr/lib/jvm/${jdk}/bin/$c
+ update-alternatives --install /usr/bin/$c $c ${bin} ${priority}
+ echo "jdkhl $c ${bin}" >>${jinfo}
+update-java-alternatives -s ${jdk}