I recently commenced the third installation of JAVA on one of my Debian-Boxes and  couldn’t remember any of the exact commands, had to google again, ended up with 8 open tabs all saying something different and so wrote this small bash script to set up all necessary alternatives with “update-alternatives” automatically.

Please note that there has to be either icedtea or openjdk installed to make all parts of this script work.

Installation instructions

  1. grab your JDK/JRE either from  http://www.oracle.com/technetwork/java/javase/downloads/index.html (JDK) or http://www.java.com/en/ (JRE only)
  2. choose the linux tgz version for your architecture
  3. run as root:apt-get install default-jre default-jdk
  4. unpack the downloaded tgz from 1. to a safe location (I prefer /opt/java )
  5. run the script below pointing to the extracted java directory (e.g. install.sh /opt/java/foo)

Enjoy!

Download/view script over at pastebin

#!/bin/bash
#script to install legacy java on a debian/ubuntu box
#Author: Jan Kapellen <jkapellen@gmail.com>

usage() {
        echo "Usage: $0 /path/to/java/dir"
        exit 1
}

[[ $# -eq 0 ]] && usage

PRIO=1065

JAVA=$1/bin/java
if [[ -f $JAVA ]]
then    
        echo -n "installing java version "
        $JAVA -version
        BINPATH=$1/bin
        for bin in `ls -1 $BINPATH`
        do      
                FULLBIN=$BINPATH/$bin
                EFFBIN=`which $bin`
                if [[ $EFFBIN != "" ]]
                then    
                        #echo "$FULLBIN -> $EFFBIN -> $bin"
                        update-alternatives --install $EFFBIN $bin $FULLBIN $PRIO
                        update-alternatives --config $bin
                fi
        done    
        echo "checking for java-plugin ..."
        PLUG=`find $1 -type f -name libnpjp2.so`
        if [[ $PLUG != "" ]]
        then
                update-alternatives --install /usr/lib/mozilla/plugins/libjavaplugin.so mozilla-javaplugin.so $PLUG $PRIO
                update-alternatives --config mozilla-javaplugin.so
        else
                echo "no plugin found, skipping plugin!"
        fi

else
        echo "java binary not found in directory! exiting ..."
        exit 1
fi