Skip to content

Instantly share code, notes, and snippets.

@filipepgoes
Last active February 23, 2018 15:29
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 filipepgoes/277edea0b31506300fb36c649ca6f08d to your computer and use it in GitHub Desktop.
Save filipepgoes/277edea0b31506300fb36c649ca6f08d to your computer and use it in GitHub Desktop.
Script to install Java in Ubuntu 14 and CentOS 7
#!/bin/bash
# Installing JAVA
# Authored by Filipe Goes
# Tested in Ubuntu Server 14.05
# Tested in CentOS 7.3
set -u
usage()
{
echo "usage: java-install-sh -u 'JDK download URL' -f 'Foldername to install (ex. jdk1.8.0_121).'"
}
while getopts u:f: OPCAO; do
case "${OPCAO}" in
u) u="${OPTARG}";;
f) f="${OPTARG}";;
esac
done
if [ -z ${u+x} ];then
echo "ERROR: Java URL not found. Aborting."
exit 1
fi
if [ -z ${f+x} ];then
echo "ERROR: Java foldername not supplied. Aborting."
exit 1
fi
JAVA_URL=${u} # http://download.oracle.com/otn-pub/java/jdk/8u121-b13/e9e7ea248e2c4826b92b3f075a80e441/jdk-8u121-linux-x64.tar.gz
JAVA_FOLDERNAME=${f}# jdk1.8.0_121
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root."
exit 1
fi
echo "Downloading: $JAVA_URL..."
wget -O jdk.tar.gz --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" $JAVA_URL
if [ $? -ne 0 ]; then
echo "Not possible to download Java."
exit 1
fi
echo "Installation..."
tar -xvf jdk.tar.gz
echo "Cleaning up..."
rm -f jdk.tar.gz
mkdir -p /usr/lib/jvm
mv ./$JAVA_FOLDERNAME /usr/lib/jvm/
update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/$JAVA_FOLDERNAME/bin/java" 1
update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/$JAVA_FOLDERNAME/bin/javac" 1
update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/lib/jvm/$JAVA_FOLDERNAME/bin/javaws" 1
update-alternatives --install "/usr/bin/keytool" "keytool" "/usr/lib/jvm/$JAVA_FOLDERNAME/jre/bin/keytool" 1
chmod a+x /usr/bin/java
chmod a+x /usr/bin/javac
chmod a+x /usr/bin/javaws
chmod a+x /usr/bin/keytool
chown -R root:root /usr/lib/jvm/$JAVA_FOLDERNAME
echo "Done. Reboot to before installing the container. Run 'update-alternatives --config java' if necessary."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment