bash script to install java on Linux


Install JDK 7 or JDK 8 by Shell Script on Linux

Create a file on root directory with name of jdk-install.sh and give execute permission.

# touch /root/jdk-install.sh
# chmod +x jdk-install.sh
#sh jdk-install.sh  jdk-archive-file.tar.gz

######################
#!/bin/bash
ORACLE_DOWNLOAD_LINK=http://www.oracle.com/technetwork/java/javase/downloads/index.html
##########
if [ "$#" = 0 ]
then
echo "Usage:$0 [options] jdk-archive-file.tar.gz"
exit 1
fi
JDK_ARCHIVE=$1
#Check if the script is running with root permissions
if [ `id -u` -ne 0 ]; then
   echo "The script must be run as root! (you can use sudo)"
   exit 1
fi
#   Is the file a valid archive?
echo -n "Validating the archive file... "
gunzip -t $JDK_ARCHIVE 2>> /dev/null
if [ $? -ne 0  ]; then
   echo "FAILED"
   echo
   echo "Provided file is not a valid .tar.gz archive: $JDK_ARCHIVE"
   echo
   echo "Be sure to download Linux .tar.gz package from the Oracle website"
   echo $ORACLE_DOWNLOAD_LINK
   echo
   exit 1
fi
#################
#   Is the file containing JDK?
#   Also obtain JDK version using the occassion
JDK_VERSION=`tar -tf $JDK_ARCHIVE | egrep '^[^/]+/$' | head -c -2` 2>> /dev/null
if [[ $JDK_VERSION != "jdk"* ]]; then
   echo "FAILED"
   echo
   echo "The provided archive does not contain JDK: $JDK_ARCHIVE"
   echo
   echo "Please provide valid JDK archive from Oracle Website"
   echo $ORACLE_DOWNLOAD_LINK
   echo
   exit 1
fi
echo "OK"
# All checks are done at this point
# Begin Java installation
# Extract the archive
echo -n "Extracting the archive... "
JDK_LOCATION=/usr/lib/jvm/$JDK_VERSION
mkdir -p /usr/lib/jvm
tar -xf $JDK_ARCHIVE -C /usr/lib/jvm
echo "OK"
# Update system to use Oracle Java by default
echo -n "Updating system alternatives... "
update-alternatives --install "/usr/bin/java" "java" "$JDK_LOCATION/jre/bin/java" 1 >> /dev/null
update-alternatives --install "/usr/bin/javac" "javac" "$JDK_LOCATION/bin/javac" 1 >> /dev/null
update-alternatives --set java $JDK_LOCATION/jre/bin/java >> /dev/null
update-alternatives --set javac $JDK_LOCATION/bin/javac >> /dev/null
echo "OK"
# Verify and exit installation
echo -n "Verifying Java installation... "
JAVA_CHECK=`java -version 2>&1`
if [[ "$JAVA_CHECK" == *"Java(TM) SE Runtime Environment"* ]]; then
   echo "OK"
   echo
   echo "Java is successfully installed!"
   echo
   java -version
   echo
   exit 0
else
   echo "FAILED"
   echo
   echo "Java installation failed!"
   echo
   exit 1
fi
#######################

Comments

Popular Posts

Install and configure rsyslog Centralized logging server in CentOS

How to fix postfix/smtp Network is unreachable error

Could not join realm: Necessary packages are not installed: sssd-tools sssd libnss-sss libpam-sss adcli