Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What's is the most efficient way to install multiple concurrent Java JDK versions on the same Linux machine?

I need Java5 whilst my current Ubuntu 9.10 Karmic comes loaded with Java6.

I know I could setup a VM for this but I am looking for alternatives.

share|improve this question
up vote 2 down vote accepted

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.

share|improve this answer
Thanks but I need concurrent installs. How do I do that? – jldupont Apr 28 '10 at 14:49
thanks. I have now installed JDK5 in /opt and wrote myself a bash script to setup the environment variables... it looks has it works. I thought it would be more painful than this! – jldupont Apr 28 '10 at 15:09
@jldupont: Actually that's one of the things I love about linux...Since there is no registry to get in the way, you can install the same thing a hundred times, and it's not a problem as long as you're making sure to not over-write common files. I know a lot of people who run (for example) multiple different versions of Apache. I myself tend to run multiple versions of Perl and Java. – Satanicpuppy Apr 28 '10 at 15:29

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:

# 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"

case "$1" in
    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
    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}"

exit 0
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .