Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Alternative management for Java
#!/usr/bin/env bash
# Inspired by:
# https://stackoverflow.com/a/53902806/4905625
set -e
usage () {
echo "Usage: $0 {register|install} JAVA_DIR [PRIORITY]"
echo
echo "Register or activate an alternative Java version"
echo
echo "NOTE: Probably requires superuser privileges - run as sudo"
}
if [ -z "$1" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ] || [ -z "$2" ] || [ ! -z "$4" ]
then
usage
fi
DIRECTIVE="$1"
JAVA_DIR="$2"
case "$DIRECTIVE" in
register)
PRIORITY="$3"
if [ -z "$PRIORITY" ]; then usage; echo "ERROR: register command requires PRIORITY"; exit 1; fi
for binary in $JAVA_DIR/bin/*
do
binary_path=`realpath $binary`
if [ -x "$binary_path" ]
then
binary_name=`basename "$binary_path"`
update-alternatives --install "/usr/bin/$binary_name" "$binary_name" "$binary_path" "$PRIORITY"
fi
done
;;
activate)
for binary in $JAVA_DIR/bin/*
do
binary_path=`realpath $binary`
if [ -x "$binary_path" ]
then
binary_name=`basename "$binary_path"`
update-alternatives --set "$binary_name" "$binary_path"
fi
done
;;
*)
usage
exit 2
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.