Skip to content

Instantly share code, notes, and snippets.

@nottsknight
Last active April 27, 2017 20:54
Show Gist options
  • Save nottsknight/4586530 to your computer and use it in GitHub Desktop.
Save nottsknight/4586530 to your computer and use it in GitHub Desktop.
BASH script to install the Java Development Kit. User must download the JDK themselves, and then give the path to the downloaded .tar.gz archive as the only command-line argument.At the end of the script, a series of menus appear, presenting all the different places where the system can find the 'java' (or javac, etc.) command in a numbered list…
#!/usr/bin/env bash
# Abort if not super user
if [[ ! `whoami` = "root" ]]; then
echo "You must have administrative privileges to run this script"
echo "Try 'sudo ./install-java.sh'"
exit 1
fi
# Check that the file is a JDK archive
if [[ ! $1 =~ jdk-[0-9]{1}u[0-9]{1,2}.*\.tar\.gz ]]; then
echo "'$1' doesn't look like a JDK archive."
echo "The file name should begin 'jdk-XuYY', where X is the version number and YY is the update number."
echo "Please re-name the file, or download the JDK again and keep the default file name."
exit 2
fi
# Unpack the JDK, and get the name of the unpacked folder
tar -xf $1
JDK_VER=`echo $1 | sed -r 's/jdk-([0-9]{1}u[0-9]{1,2}).*\.tar\.gz/\1/g'`
JDK_NAME=`echo $JDK_VER | sed -r 's/([0-9]{1})u([0-9]{1,2})/jdk1.\1.0_\2/g'`
# Create a location to save the JDK, and move it there
mkdir /usr/local/java
touch /usr/bin/java /usr/bin/javac /usr/bin/javaws /usr/bin/jar
mv $JDK_NAME /usr/local/java
JAVA_HOME=/usr/local/java/$JDK_NAME
# Place links to java commands in /usr/bin, and set preferred sources
update-alternatives --install "/usr/bin/java" "java" "$JAVA_HOME/bin/java" 1
update-alternatives --set "java" "$JAVA_HOME/bin/java"
update-alternatives --install "/usr/bin/javac" "javac" "$JAVA_HOME/bin/javac" 1
update-alternatives --set "javac" "$JAVA_HOME/bin/javac"
update-alternatives --install "/usr/bin/javaws" "javaws" "$JAVA_HOME/javaws" 1
update-alternatives --set "javaws" "$JAVA_HOME/bin/javaws"
update-alternatives --install "/usr/bin/jar" "jar" "$JAVA_HOME/bin/jar" 1
update-alternatives --set "jar" "$JAVA_HOME/bin/jar"
# Affirm completion, optionally delete archive, and exit
echo "Java Development Kit version $JDK_VER successfully installed!"
echo -n "Delete the archive file '$1'? [y/N] "
confirm=""
while [[ $confirm != "n" && $confirm != "N" && $confirm != "y" && $confirm != "Y" ]]; do
read confirm
if [[ $confirm = "y" || $confirm = "Y" ]]; then
rm $1
fi
done
exit 0
@nottsknight
Copy link
Author

Did some tweaking to fix syntactical errors in regular expressions (lines 7, 20)

@nottsknight
Copy link
Author

Re-wrote condition at start to automatically exit if file name doesn't match the default.

@wnan42
Copy link

wnan42 commented May 14, 2015

Great work. One tiny mistake, in line 36 "$JAVA_HOME/javaws" should be "$JAVA_HOME/bin/javaws"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment