Multiple Java environment on the same Linux machine

If you download the linux binary from Sun you can install it in whatever directory you like. Then just reference those libs in your code, and you're good to go.

With the binary installer, it'll create its own named subdirectory (e.g. in your example, /usr/java/jdk1.5.0_), so you can download as many as you want, and they'll line themselves up in appropriately named sub-drectories.

The main java binary lives in /usr/bin, so if you want to replace that to the point where when you type "java" it accesses your java, and not that one, you just move the old one out of /usr/bin, and link your new one in there. Typing which java will tell you what the default java on your system is.

@jldupont: When I think of concurrent installs, I think of multiple versions installed on the same machine, which my method will absolutely give you. I have about 12 versions of java installed on my production box to handle hand-me-downs from corporate that haven't yet been updated.

However when you type "java" you're only going to get one version of java, since that's what's in '/usr/bin'...You'd have to type something like '/usr/java/jdk1.5.1/bin/java' to get a specific java binary that's not the system default.


Just download and execute the .bin file from oracle which then extracts itself into a folder. Move this folder (e.g. jdk1.6.0_32) into /usr/lib/jvm and then the script setjava I use to change java versions. Because of the exportstatements you need to stay in the same terminal, however. Also some small jvm tools may not be set but I guess it should work in most cases. Also I'm a shell noob so please correct me if there is a better possibility :-) The code for me is:

#!/bin/bash
# shouldnt be used in a loop, else the path may become too long

if [[ $(/usr/bin/id -u) -ne 0 ]]; then
    echo "Not running as root"
    exit
fi

case "$1" in
  6)
    echo "Setting Java version 6"
    export PATH=/usr/lib/jvm/jdk1.6.0_32/bin:$PATH
    export JAVA_HOME=/usr/lib/jvm/jdk1.6.0_32
    sudo rm /usr/bin/java
    sudo rm /usr/bin/javac
    sudo rm /usr/bin/javadoc
    sudo rm /usr/bin/javah
    sudo rm /usr/bin/javap
    sudo rm /usr/bin/javaws
    sudo ln -s /usr/lib/jvm/jdk1.6.0_32/bin/java /usr/bin/java
    sudo ln -s /usr/lib/jvm/jdk1.6.0_32/bin/javac /usr/bin/javac
    sudo ln -s /usr/lib/jvm/jdk1.6.0_32/bin/javadoc /usr/bin/javadoc
    sudo ln -s /usr/lib/jvm/jdk1.6.0_32/bin/javah /usr/bin/javah
    sudo ln -s /usr/lib/jvm/jdk1.6.0_32/bin/javap /usr/bin/javap
    sudo ln -s /usr/lib/jvm/jdk1.6.0_32/bin/javaws /usr/bin/javaws
    javac -version    
    java -version
    ;;
  7)
    echo "Setting Java version 7"
    export PATH=/usr/lib/jvm/java-7-openjdk/bin:$PATH
    export JAVA_HOME=/usr/lib/jvm/java-7-openjdk/
    sudo rm /usr/bin/java
    sudo rm /usr/bin/javac
    sudo rm /usr/bin/javadoc
    sudo rm /usr/bin/javah
    sudo rm /usr/bin/javap
    sudo rm /usr/bin/javaws
    sudo ln -s /usr/lib/jvm/java-7-openjdk/bin/java /usr/bin/java
    sudo ln -s /usr/lib/jvm/java-7-openjdk/bin/javac /usr/bin/javac
    sudo ln -s /usr/lib/jvm/java-7-openjdk/bin/javadoc /usr/bin/javadoc
    sudo ln -s /usr/lib/jvm/java-7-openjdk/bin/javah /usr/bin/javah
    sudo ln -s /usr/lib/jvm/java-7-openjdk/bin/javap /usr/bin/javap
    sudo ln -s /usr/lib/jvm/java-7-openjdk/bin/javaws /usr/bin/javaws
    javac -version
    java -version
    ;;
  *)
    echo "Usage: $0 {6|7}"

esac
exit 0