Lets see how to install or update Oracle JDK 7 in semiautomatic way under Linux operating systems, including Ubuntu, Debian and CentOS. Semi is because Oracle has its proprietary license you have to accept before downloading.
Install or Update Oracle JDK 7 in Ubuntu
- So the first step is to download JDK from Oracle’s site.
- Create linux executable file ‘oracle_jdk7_install.sh’
- Put following content on this file. Just copy & paste
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990#!/bin/bashINSTALL_DIR=/usr/lib/jvmJDK7_DIR=$INSTALL_DIR/java-7-oracleecho "The script installs Oracle JDK 7 from tar.gz file."if [[ $EUID -ne 0 ]]; thenecho "This script must be run as root."exit 1fiecho "Looking for JDK archive..."LATEST_JDK_ARCHIVE=`ls jdk-7u*-linux-*.tar.gz | sort | tail -1`UPDATE=`echo $LATEST_JDK_ARCHIVE | awk -F '-' '{print $2}' | awk -F '7u' '{print $2}'`if [ -z "$LATEST_JDK_ARCHIVE" ] || [ -z "$UPDATE" ]; thenecho "JDK archive wasn't found. It should be in the current directory."exit 1fiecho "Found archive: $LATEST_JDK_ARCHIVE"echo "Extracting archive..."tar -xzf $LATEST_JDK_ARCHIVEif [ $? -ne 0 ]; thenecho "Error while extraction archive."exit 1fiif [ ! -d jdk1.7.0_$UPDATE ]; thenecho "Unexpected archive content."exit 1fiecho "Moving content to installation directory..."[ ! -e $INSTALL_DIR ] && mkdir -p $INSTALL_DIR[ -e $INSTALL_DIR/jdk1.7.0_$UPDATE ] && rm -r $INSTALL_DIR/jdk1.7.0_$UPDATE/[ -e $JDK7_DIR ] && rm $JDK7_DIRmv jdk1.7.0_$UPDATE/ $INSTALL_DIRln -sf $INSTALL_DIR/jdk1.7.0_$UPDATE/ $JDK7_DIRln -sf $INSTALL_DIR/jdk1.7.0_$UPDATE/ $INSTALL_DIR/default-javaecho "Updating alternatives..."# The following part has been taken from script# http://webupd8.googlecode.com/files/update-java-0.5b# and modified to make it work without X-server.gzip -9 $JDK7_DIR/man/man1/*.1 >/dev/null 2>&1 &LATEST=$((`update-alternatives --query java|grep Priority:|awk '{print $2}'|sort -n|tail -1`+1));if [ -d "$JDK7_DIR/man/man1" ];thenfor f in $JDK7_DIR/man/man1/*; doname=`basename $f .1.gz`;#some files, like jvisualvm might not be links. Further assume this for corresponding man pageif [ ! -f "/usr/bin/$name" -o -L "/usr/bin/$name" ]; thenif [ ! -f "$JDK7_DIR/man/man1/$name.1.gz" ]; thenname=`basename $f .1`; #handle any legacy uncompressed pagesfiupdate-alternatives --install /usr/bin/$name $name $JDK7_DIR/bin/$name $LATEST \--slave /usr/share/man/man1/$name.1.gz $name.1.gz $JDK7_DIR/man/man1/$name.1.gzfidone#File links without man pages[ -f $JDK7_DIR/bin/java_vm ] && update-alternatives --install /usr/bin/java_vm \java_vm $JDK7_DIR/jre/bin/java_vm $LATEST[ -f $JDK7_DIR/bin/jcontrol ] && update-alternatives --install /usr/bin/jcontrol \jcontrol $JDK7_DIR/bin/jcontrol $LATESTelse #no man pages availablefor f in $JDK7_DIR/bin/*; doname=`basename $f`;#some files, like jvisualvm might not be linksif [ ! -f "/usr/bin/$name" -o -L "/usr/bin/$name" ]; thenupdate-alternatives --install /usr/bin/$name $name $JDK7_DIR/bin/$name $LATESTfidonefiecho "Setting up Mozilla plugin..."#File links that apt-get misses[ -f $JDK7_DIR/bin/libnpjp2.so ] && update-alternatives --install \/usr/lib/mozilla/plugins/libnpjp2.so libnpjp2.so $JDK7_DIR/jre/lib/i386/libnpjp2.so $LATESTecho "Setting up env. variable JAVA_HOME..."cat > /etc/profile.d/java-home.sh << "EOF"export JAVA_HOME="/usr/lib/jvm/java-7-oracle"export PATH="$JAVA_HOME/bin:$PATH"EOFecho "Checking version..."java -versionecho "Done." - Run the script as sudoer:
123sudo bash oracle-jdk-7-update.shORsudo ./oracle_jdk7_install.sh
If in the end of output you see something like
1 2 3 |
java version "1.7.0_51" Java(TM) SE Runtime Environment (build 1.7.0_51-b13) Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode) |
then Oracle JDK 7 was successfully installed.