Changing Java Executable Program Using alternative Command in Linux

Posted on

Operating System : CentOS 7

Java : JDK 1.8.0_91

In order for several utilities or programs to run normally some of them need to be executed on top of Java Runtime Environment. Sometime we need to install different version of Java Runtime Environment from the one which has already installed as default Java Runtime Environment in order for those utilities or programs run well. Below is the step which has to be done :

  1. First of all, we have to install Java Runtime Environment which I preferably do that by downloading portable compressed file of Java Standard Development Kit which is provided from the official site of Java available. In the past, I usually visit Sun Microsystem’s website to look for the compatible JDK, but nowadays since the Sun Microsystem’s acquisition by Oracle has already happened, the official JDK can now be found in Oracle’s official website. Normally, I put all utilities or program which is installed by extracting it in /opt, so the command for installing Java Runtime Environment which also regularly come as a package with Java Development Kit.

  1. List the available java configuration alternatives by running the following command :
alternatives --config java

It will display the available alternatives which can be used as Java utilities or program in the operating system.

[username@hostname opt]# alternatives --config java
There are 2 programs which provide 'java'.
Selection    Command
-----------------------------------------------
*  1           /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.65-2.b17.el7_1.x86_64/jre/bin/java
+ 2           /opt/jdk1.8.0_60/bin/java
The asterisk sign imply that the currently used as /usr/bin/java executable program of java is being installed in /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.65-2.b17.el7_1.x86_64/jre/bin/java
  1. Since we want to add another alternatives of executable Java Runtime Environment which has already been installed in the first step, we have to execute the following command :
alternatives --install /usr/bin/java java_development_kit_installation_path number_of_entry
--install is a execution parameter of the alternatives command which is implying that we want to install an alternatives of how we will execute java.
/usr/bin/java as the main executable program of java.
java_development_kit_installation_path is the alternative path of the execution command of java and it is defined after the /usr/bin/java
number_of_entry is the number of entry which is going to be used as list of program alternative.

Below is an example :

Supposed we have already installed Java Development Kit in /opt/jdk1.8.0_91 and we want to use it as default executable program of java, we have to run the following command :

[username@hostname opt]# alternatives --install /usr/bin/java /opt/jdk1.8.0_91/bin/java 3
alternatives version 1.3.61 - Copyright (C) 2001 Red Hat, Inc.
his may be freely redistributed under the terms of the GNU Public License.
usage: alternatives --install <link> <name> <path> <priority>
[--initscript <service>]
[--slave <link> <name> <path>]*
alternatives --remove <name> <path>
alternatives --auto <name>
alternatives --config <name>
alternatives --display <name>
alternatives --set <name> <path>
alternatives --list
common options: --verbose --test --help --usage --version
--altdir <directory> --admindir <directory>
[username@hostname opt]# alternatives --install /usr/bin/java java /opt/jdk1.8.0_91/bin/java 3

The above command specify that we are installing java executable program in /usr/bin/java with an alternative specified in /opt/jdk1.8.0_91/bin/java in the entry list number 3.

  1. The next thing is we need to check the already installed Java Runtime Environment or JDK has already made in the list of java alternative by executing the following command :
alternatives --config java
[username@hostname opt]# alternatives --config java
There are 3 programs which provide 'java'.
Selection    Command
-----------------------------------------------
*  1           /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.65-2.b17.el7_1.x86_64/jre/bin/java
+ 2           /opt/jdk1.8.0_60/bin/java
3           /opt/jdk1.8.0_91/bin/java
Enter to keep the current selection[+], or type selection number: 3
[username@hostname opt]#

The entry has already been made and it is actually appended the previous list and it is installed in the 3rd (third) entry list.

  1. As shown in the above output, there are three entries of Java configuration which can be used. And we have to type the number of the list in the question provided generated by the command. In the above display, I type number 3 to select the third entry as the default Java configuration which can be used to execute any kinds of utilities or programs needs Java.

Type again the following command to check whether the default executable /usr/bin/java has already changed to the 3rd (third) entry :

[username@hostname opt]# alternatives --config java
There are 3 programs which provide 'java'.
Selection    Command
-----------------------------------------------
1           /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.65-2.b17.el7_1.x86_64/jre/bin/java
+ 2           /opt/jdk1.8.0_60/bin/java
* 3           /opt/jdk1.8.0_91/bin/java
The asterisk sign which mark the default executable java program has already changed into the 3rd (third) entry.
  1. To even further prove it, we can execute the java program and check its version by typing the following command :
java -version
[username@hostname opt]# java -version
java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)
[username@hostname opt]#

Leave a Reply