21 March 2018 | 2 min read

Using alternative utils with JRE & JDK

This is just side note coz sometime I forgot how to do it..pfft


The /usr/sbin/alternatives is a tool for managing different software packages that provide the same functionality, for example; different version of JRE and JDK. From manual it said to creates, removes, maintains and displays information about the symbolic links comprising the alternatives system. The alternatives system is a reimplementation of the Debian alternatives system.

As Java developer I need different kind runtime of JRE and JDK enviroment during development and testing,this functionality very suitable to me.

Which JRE or JDK package I need, RPM or tarball?

First of all, you should beware not to use Oracle RPM, as it will be problematic with OpenJDK default package. So grab tarball from official JAVA website and click on the Download button.), then unpack to /usr/java/ directory :

$ cd /usr/java/
$ sudo tar zxvf jre-9.0.4_linux-x64_bin.tar.gz
$ sudo tar zxvf jdk-8u161-linux-x64.tar.gz
$ sudo rm *.gz

Adding alternative - another version of java and javac

After you extracted the tarball, use install action flag to add it into java group :

$ sudo alternatives --install /usr/bin/java java /usr/java/jre1.8.0_161/bin/java 1

Same thing goes with javac group also. Don’t forget to change the path to JDK :

$ sudo alternatives --install /usr/bin/javac javac /usr/java/jdk1.8.0_161/bin/javac 1

It better to symlink any recent version as default and latest :

$ sudo ln -s /usr/java/jdk1.8.0_161/ /usr/java/latests
$ sudo ln -s /usr/java/latests /usr/java/default

So, you suppose to get something like this:

$ ls -l /usr/java | grep -E '(default|latest)'
lrwxrwxrwx. 1 root root   16 Aug 29  2017 default -> /usr/java/latest
lrwxrwxrwx  1 root root   23 Mar 20 14:00 latest -> /usr/java/jdk1.8.0_161/

Selecting alternative

Now you can choose which java and javac version to use :

$ sudo alternatives --config java
$ sudo alternatives --config javac

(Insert any number from the selection list or press enter to using the current)

Now you installed JRE and JDK or multiple version and you can configure your system to use one or another easier with alternatives command.

Thank for reading, see ya!

Robbi Nespu | Centos, Debian, Fedora, Java, Linux, Ubuntu


Discussion and feedback