Snippets

Marco Massenzio Support multiple JDKs on Ubuntu via the update-java-alternatives facility.

Updated by Marco Massenzio

File update-jdk Modified

  • Ignore whitespace
  • Hide word diff
     /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
+    
+    Author: Marco Massenzio (https://codetrips.com) (c) 2020.  All rights reserved.
+    Released under the Apache 2.0 License (https://www.apache.org/licenses/LICENSE-2.0.html)
 "
 }
 
Created by Marco Massenzio

File update-jdk Added

  • Ignore whitespace
  • Hide word diff
+#!/bin/bash
+#
+# Sets up a new Java JDK
+# Loosely based on https://askubuntu.com/questions/1079239/can-not-install-jdk11-using-update-alternatives-install
+
+set +eu
+
+function usage {
+    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
+    usage
+    exit 0
+fi
+
+
+jdk=${1:-}
+priority=${2:-1010}
+alias=${3:-${jdk}}
+jinfo="/usr/lib/jvm/.${jdk}.jinfo"
+
+if [[ -z ${jdk} || ! -d /usr/lib/jvm/${jdk} ]]; then
+    usage
+    exit 1
+fi
+
+cat <<EOF >${jinfo}
+name=${jdk}
+alias=${alias}
+priority=${priority}
+section=main
+
+EOF
+
+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}
+done
+
+update-java-alternatives -s ${jdk}
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.