Skip to content

Instantly share code, notes, and snippets.

@mdaniel
Last active December 8, 2015 21:40
Show Gist options
  • Save mdaniel/6b058ac06153d853c7ea to your computer and use it in GitHub Desktop.
Save mdaniel/6b058ac06153d853c7ea to your computer and use it in GitHub Desktop.
Uses the Debian/Ubuntu update-alternatives system to wire in a Sun/Oracle JDK as preferential
#! /bin/sh
set -e
# --install link name path priority
# --slave link name path
## link := the fully-qualified symlink path /usr/bin/java
## name := `basename $link` (for the most part)
## path := the fully-qualified alternative path
## openjdk priority is 1700017
target_java=$1
if test ! -e $target_java
then
echo "Your target_java ($1) does not exist" >&2
exit 1
fi
if test ! -x $target_java/bin/java
then
echo "Your target_java ($1) does not contain java" >&2
exit 1
fi
target_jre=$target_java/jre
if test ! -d $target_jre
then
echo "Your target_jre ($target_jre) is not a directory" >&2
exit 1
fi
if test ! -x $target_jre/bin/java
then
echo "Your target_jre ($target_jre) does not contain java" >&2
exit 1
fi
java_version_full=$($target_java/bin/java -version 2>&1 | sed -ne 's/java version "\([^"]*\)"/\1/p')
java_version_triplet=$(echo $java_version_full | sed -e 's/_.*//')
if test -z "$priority"; then
# ensure ours is the largest number,
# and openjdk has the "patch number" as 000 not _00 as Sun/Oracle does
priority=9$(echo $java_version_full | sed -e 's/_/0/' -e 's/[^0-9]//g')
fi
update_java_home_alts() {
$ECHO update-alternatives --install \
/usr/lib/jvm/java java_home $target_java $priority
$ECHO update-alternatives --install \
/usr/lib/jvm/java-$java_version_triplet java-$java_version_triplet $target_java $priority
$ECHO update-alternatives --install \
/usr/lib/jvm/jre jre_home $target_jre $priority
$ECHO update-alternatives --install \
/usr/lib/jvm/jre-$java_version_triplet jre-$java_version_triplet $target_jre $priority
}
update_java_bin_alts() {
for i in /usr/lib/jvm/java/bin/*
do
have_man=0
slave=''
bn=`basename $i`
if test '*' == "$bn"; then
break
fi
mf=$bn.1
mp=/usr/lib/jvm/java/man/man1
if test -f "$mp/$mf"; then
have_man=1
elif test -f "$mp/$mf.gz"; then
have_man=1
mf=$mf.gz
fi
if test 1 -eq $have_man; then
slave="--slave /usr/share/man/man1/$mf $mf $mp/$mf"
fi
$ECHO update-alternatives --install \
/usr/bin/$bn $bn $i $priority $slave
done
}
update_java_home_alts
update_java_bin_alts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment