#!/bin/bash## Sets up a new Java JDK# Loosely based on https://askubuntu.com/questions/1079239/can-not-install-jdk11-using-update-alternatives-installset +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
exit0fijdk=${1:-}priority=${2:-1010}alias=${3:-${jdk}}jinfo="/usr/lib/jvm/.${jdk}.jinfo"if[[ -z ${jdk}|| ! -d /usr/lib/jvm/${jdk}]];then usage
exit1ficat <<EOF >${jinfo}name=${jdk}alias=${alias}priority=${priority}section=mainEOFfor c in $(ls /usr/lib/jvm/${jdk}/bin);dobin=/usr/lib/jvm/${jdk}/bin/$c update-alternatives --install /usr/bin/$c$c${bin}${priority}echo"jdkhl $c${bin}" >>${jinfo}doneupdate-java-alternatives -s ${jdk}
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.