Skip to content

Instantly share code, notes, and snippets.

@vsee
Last active October 8, 2019 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vsee/1d260fa35ffc05bc7f1ead2b391d8748 to your computer and use it in GitHub Desktop.
Save vsee/1d260fa35ffc05bc7f1ead2b391d8748 to your computer and use it in GitHub Desktop.
Java version switcher for Ubuntu
#!/bin/bash
# Swaps between java versions
#
# Execute using the source or '.' command to set environment correctly
#
# to install alternatives after unpacking oracle java tar at /opt/jdk1.8.0_202/ execute
# sudo sh -c 'for bin in /opt/jdk1.8.0_202/bin/*; do update-alternatives --install /usr/bin/$(basename $bin) $(basename $bin) $bin 1500; done'
#
# to switch manually for individual bins execute
# sudo update-alternatives --config java
VERSION=$1
if [ -z "$VERSION" ]; then
echo "ERROR: specify Java version to be 8 or 11."
exit 1
fi
echo "Activating java version" $VERSION
if [ $VERSION -eq 11 ]; then
sudo sh -c 'for bin in /opt/jdk-11.0.4/bin/*; do update-alternatives --set $(basename $bin) $bin; done'
export JAVA_HOME="/opt/jdk-11.0.4/"
elif [ $VERSION -eq 8 ]; then
sudo sh -c 'for bin in /opt/jdk1.8.0_202/bin/*; do update-alternatives --set $(basename $bin) $bin; done'
export JAVA_HOME="/opt/jdk1.8.0_202/"
else
echo "ERROR: unsupported version specified:" $VERSION
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment