Problem scenario
You want a Bash script to install Java on Linux. (If you want to install Java on Windows, see this posting.) You want it to work regardless if it is a Linux that is part of the Debian/Ubuntu family, RedHat family (e.g., CentOS/Fedora), or SUSE. What do you do to write a universal script to make it easy for other systems administrators to install Java (or potentially automate the process across several servers)?
Solution
1. Create a script. Call it java.sh and place it in the /tmp/ directory. Here is the content of the script to install Java:
#!/bin/bash
# Written by www.continualintegration.com
distro=$(cat /etc/*-release | grep NAME)
debflag=$(echo $distro | grep -i "ubuntu")
if [ -z "$debflag" ]
then # If it is not Ubuntu, test if it is Debian.
debflag=$(echo $distro | grep -i "debian")
echo "determining Linux distribution..."
else
echo "You have Ubuntu Linux!"
fi
rhflag=$(echo $distro | grep -i "red*hat")
if [ -z "$rhflag" ]
then #If it is not RedHat, see if it is CentOS or Fedora.
rhflag=$(echo $distro | grep -i "centos")
if [ -z "$rhflag" ]
then #If it is neither RedHat nor CentOS, see if it is Fedora.
echo "It does not appear to be CentOS or RHEL..."
rhflag=$(echo $distro | grep -i "fedora")
fi
fi
if [ -z "$rhflag" ]
then
echo "...still determining Linux distribution..."
else
echo "You have a RedHat distribution (e.g., CentOS, RHEL, or Fedora)"
yum -y install java-1.8.0-openjdk*
JAVA_HOME=/usr/lib/jvm/jre-1.8.0-openjdk
echo 'export JAVA_HOME=/usr/lib/jvm/jre-1.8.0-openjdk' >> ~/.bashrc
source ~/.bashrc
source /etc/environment
fi
if [ -z "$debflag" ]
then
echo "...still determining Linux distribution..."
else
echo "You are using either Ubuntu Linux or Debian Linux."
apt-get -y update # This is necessary on new AWS Ubuntu servers.
apt-get -yo Dpkg::Options::="--force-overwrite" install openjdk-11-jdk
apt-get -y install openjdk-11-source
echo 'JAVA_HOME=/usr/lib/jvm/java-11-openjdk' >> ~/.bashrc
echo 'PATH=$PATH:/usr/lib/jvm/java-11-openjdk/bin' >> ~/.bashrc
source ~/.bashrc
source /etc/environment
fi
suseflag=$(echo $distro | grep -i "suse")
if [ -z "$suseflag" ]
then
if [ -z "$debflag" ]
then
if [ -z "$rhflag" ]
then
echo "*******************************************"
echo "Could not determine the Linux distribution!"
echo "Installation aborted. Nothing was done."
echo "******************************************"
exit
fi
fi
else
zypper -n install java-1_8_0-openjdk java-1_8_0-openjdk-devel
fi
2. Run it like this:sudo bash /tmp/java.sh
3. You are done. Please read this last section only if you are trying to deploy Apache Spark 2.2.1. In AWS the Ubuntu repos support version 9. But Spark 2.2.1 needs Java 8 and won't work with Java 9; you may need to change the script if you are trying to deploy Spark 2.2.1.
To have a script install Java 8 on Ubuntu or Debian Linux, replace these consecutive lines in the script (in an else/fi block for Ubuntu/Debian Linux):
echo "You are using either Ubuntu Linux or Debian Linux."
apt-get -y update # This is necessary on new AWS Ubuntu servers.
apt-get -o Dpkg::Options::="--force-overwrite" install openjdk-11-jdk
apt-get -y install openjdk-11-source
echo 'JAVA_HOME=/usr/lib/jvm/java-11-openjdk' >> ~/.bashrc
echo 'PATH=$PATH:/usr/lib/jvm/java-11-openjdk/bin' >> ~/.bashrc
source ~/.bashrc
source /etc/environment
To be these lines:
echo "You are using either Ubuntu Linux or Debian Linux."
apt-get -y update # This is necessary on new AWS Ubuntu servers.
apt -y install openjdk-8-jre-headless