Bash Script for Installing / Updating Java JDK RPM

by | Mar 11, 2016 | Code, Server | 0 comments

Here’s a BASH script I wrote which downloads and installs the Java JDK RPM. Tested on CentOS 6.x. If you are looking to install on Ubuntu (or Mint) go here.

It also downloads the Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files which are required for some applications.

You must replace the version variables in order to download the latest version. Here are the instructions for finding these.

  1. To find them go here and under the Java category go to Java SE.
  2. Click the big Java Download near the top.
  3. Click Accept License Agreement to view the links for the downloads.
  4. Find the Linux x64 if you are on a 64 bit OS or Linux i586 if you are on 32 bit. If you don’t know then you’re probably on 64 bit.
  5. Right click on the link in the right side column and go to Copy Link Location or Copy link address, etc.
  6. Now you can use this link you’ve copied to find the version numbers required. At the moment CURRENT and MINOR are not specified anywhere in the URL. Here is an image illustrating the MAJOR, REV and BUILD variables which you can update. In the future I am planning to write some regex to parse these out but haven’t yet.java
  7. Save the script below into a javainstall.sh file and make the file executable:
    chmod +x javainstall.sh
  8. Run the script to install / update java:
    ./javainstall.sh
#!/bin/bash
#
# Install Java JDK RPM
#
# https://servercode.ca/bash-script-installing-updating-java-jdk-rpm/

# Version variables
CURRENT=1
MAJOR=8
MINOR=0
REV=121
BUILD=13
# md5 hash added to URL 17th Jan 2017
HASH=e9e7ea248e2c4826b92b3f075a80e441

# Compile variables
VERSION="${MAJOR}u${REV}"
RPM="jdk-${VERSION}-linux-x64.rpm"
URL="http://download.oracle.com/otn-pub/java/jdk/${VERSION}-b${BUILD}/${HASH}/${RPM}"
JPATH="/usr/java/jdk${CURRENT}.${MAJOR}.${MINOR}_${REV}"
POLICY_FILE="jce_policy-${MAJOR}.zip"
POLICY_URL="http://download.oracle.com/otn-pub/java/jce/${MAJOR}/${POLICY_FILE}"

# Download Java
wget --no-cookies --no-check-certificate --header 'Cookie: oraclelicense=accept-securebackup-cookie' "${URL}" -O "./${RPM}"

# Install Java
yum -y localinstall "./${RPM}"

# Alternatives
alternatives --install /usr/bin/java java ${JPATH}/jre/bin/java 20000
alternatives --install /usr/bin/jar jar ${JPATH}/bin/jar 20000
alternatives --install /usr/bin/javac javac ${JPATH}/bin/javac 20000
alternatives --install /usr/bin/javaws javaws ${JPATH}/jre/bin/javaws 20000
alternatives --set java ${JPATH}/jre/bin/java
alternatives --set javaws ${JPATH}/jre/bin/javaws
alternatives --set javac ${JPATH}/bin/javac
alternatives --set jar ${JPATH}/bin/jar

# Remove download
rm "./${RPM}"

# Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files
# http://download.oracle.com/otn-pub/java/jce/8/jce_policy-8.zip
#
# Note: This is only required for some applications.
#
wget --no-cookies --no-check-certificate --header 'Cookie: oraclelicense=accept-securebackup-cookie' "${POLICY_URL}" -O "./${POLICY_FILE}"
unzip -u "./${POLICY_FILE}"
cp -f ./UnlimitedJCEPolicyJDK${MAJOR}/*.jar ${JPATH}/jre/lib/security/
rm "./${POLICY_FILE}"
rm -rf ./UnlimitedJCEPolicyJDK${MAJOR}/

 

Notes:

  • Requires wget to be installed.
  • Requires unzip (for JCE) to be installed.
  • Does not update the JAVA_HOME or PATH environment variables. You can set them to /usr/java/latest which symlinks to the proper Java version.

Please report any issues.

Hosting Recommendation

Hostinger - The Secret Weapon of Successful Site Owners

There's a reason I recommend Hostinger. After testing dozens of hosting providers, only Hostinger delivers the perfect combination of:

🔥 Blazing-fast load times
(essential for performance)
🛡️ Rock-solid uptime
(your sites earn money 24/7)
🔒 Free SSL certificates
(builds trust instantly)
💾 Automated backups
(your business is always protected)
👨‍💻 Expert support
(problems solved in minutes, not days)
⚙️ One-click WordPress setup
(seamless integration guaranteed)
💰 Starting at just $2.99/month
(premium hosting that won't break the bank)

Use my link or referral code
MILLIONDOLLARS to get 20% off !