How do I make java default to a manually installed JRE/JDK?

I have Ubuntu 12.04 amd64 installed on my machine, on the previous versions of Ubuntu it was deadly easy, now there is this command update-java-alternatives with a really bad man page.

I just have my JDK unpacked on a mounted partition like /media/mydisk/jdk, how i can force the use of that JDK instead of the one that comes in the Ubuntu repository?

What is the logic behind this update-java-alternatives ?


Solution 1:

You can do this with sudo update-alternatives :

# Adding a new alternative for "java".
sudo update-alternatives --install /usr/bin/java java /media/mydisk/jdk/bin/java 1

# Setting the new alternative as default for "java".
sudo update-alternatives --config java

You have to do this for other jdk components too. These components are appletviewer, extcheck, idlj, jar, jarsigner, java, javac, javadoc, javah, javap, javaws, jconsole, jdb, jexec, jhat, jinfo, jmap, jps, jrunscript, jsadebugd, jstack, jstat, jstatd, keytool, mozilla-javaplugin.so, native2ascii, orbd, pack200, policytool, rmic, rmid, rmiregistry, schemagen, serialver, servertool, tnameserv, unpack200, wsgen, wsimport, xjc.

For further information, you can see the man pages for update-alternatives : man update-alternatives.

The command update-java-alternatives sets alternatives for java components and it seems to use update-alternatives for this.

EDIT: The answer above targets Java 6.

  • For Java 7, you have to add jcmd to the Java 6 list.
  • For Java 8, you have to add jcmd, jjs and jdeps to the Java 6 list.
  • For Java 9, you have to add jcmd, jjs, jdeps and jshell to the Java 6 list.

Solution 2:

Note: update-java-alternatives won't work with manual installations. In any case, it doesn't look like it has any special abilities which update-alternatives doesn't have, except convenience.

As a graphical alternative to @AnwarShah's command-line method, consider using Gnome Alternatives (sudo apt-get install galternatives):

  • Start GAlternatives, select each java option, change it to manual, and Add your alternative (the example here is the IBM JRE installed in my home directory):

    enter image description here

    enter image description here

    enter image description here