Jonathan Landrum Jonathan Landrum - 1 month ago 8
Java Question

How can I have multiple versions of the Java JDK installed on my machine at the same time?

Background



I work mostly in Java and Scala on my job, but a lot of our legacy code has not yet been updated to the most recent JDK. Thus, we have to have JDK versions 5, 6, 7, and 8 installed on our development machines. Until recently, I had been using Arch Linux, but I just switched to OpenSuse Tumbleweed. The method I used to install the JDK versions was to download the RPM installer scripts from the Oracle website and install them beginning with 1.5 and working my way up to 1.8 so that the
/usr/java/latest
symbolic link would already be pointing to the correct version.

Problem



After reinstalling the aforementioned JDK versions, I attempted to point to these installations in IntelliJ IDEA. But it complained with the error "The selected directory is not a valid home for JDK". I have found a lot of resources online concerning this issue and ones like it, but none of those solutions helped in my case. Furthermore, the symbolic links in
/usr/bin
for the sundry Java executables were pointing to a conflagration of
/etc/alternatives
directives.

Answer

Solution

Apparently, the RPM scripts that Oracle releases tend to break any previous JDK versions you have installed by way of removing key JAR files from those older installations, such as tools.jar and rt.jar. This renders the formerly functioning JDK useless for developers.

My first response was to rename the installation directory prior to running the second installer script. But doing that made RPM complain about already having a version of the JDK installed when running the second script. So the hack of the hack is to install one version, rename that directory, then "remove" that version with RPM (even though it will complain about not being able to find any of the respective files associated with it). Then you are free to install the second version, repeating the previous steps for each one until you get to the latest version you wish to install.

TL;DR

Here are the steps to get multiple working JDK versions on the same machine:

1. Install the first version of the JDK using the installer script:

:~# chmod a+x jdk-1_5_0_22-linux-amd64-rpm.bin
:~# ./jdk-1_5_0_22-linux-amd64-rpm.bin -x
:~# rpm -ivh --replacepkgs jdk-1_5_0_22-linux-amd64.rpm

2. Rename the install directory:

:~# mv /usr/java/jdk1.5.0_22 /usr/java/jdk1.5.0_22-bak

3. Use RPM to uninstall that version of the JDK:

:~# rpm -e jdk-2000:1.5.0_22-fcs.x86_64

4. Rinse and repeat until all versions are installed

5. Remove the -bak from the end of the directory names

:~# mv /usr/java/jdk1.5.0_22-bak /usr/java/jdk1.5.0_22

6. Change the /usr/java symlinks to the correct target:

:~# ls -la /usr/java
default -> java7
java5 -> jdk1.5.0_22
java6 -> jdk1.6.0_45
java7 -> jdk1.7.0_80
java8 -> jdk1.8.0_101
jdk1.5.0_22
jdk1.6.0_45
jdk1.7.0_80
jdk1.8.0_101
latest -> java8

7. Change the /usr/bin symlinks to the correct target:

:~# ls -la /usr/bin/ja*
jar -> /usr/java/default/bin/jar
java -> /usr/java/default/bin/java
javac -> /usr/java/default/bin/javac
javadoc -> /usr/java/default/bin/javadoc
javah -> /usr/java/default/bin/javah
javaws -> /usr/java/default/bin/javaws
Comments