Config Default Java In Linux

Overview

We can configure default Java to run in Linux using startup scripts (e.g., .bash_profile) or the update-alternatives tool, steps will be explained with details. This is a common problem when there are multiple versions of jdk to run, I ll explain how do we tell the shell to run the specific version of java we want.

Configure Default Java to Run using bash_profile and PATH

  1. install java into say /path/to/jdk1.8_0_xx/
  2. edit your .bash_profile under your home diretory and add the following scripts
JAVA_HOM=/path/to/jdk_1.8_0_xxx
export JAVA_HOME
PATH=$PATH:$JAVA_HOME/bin
export PATH

So what happens here is .bash_profile will be run after we login into shell ( see What Startup Files Are Read by the Shell for more details), and in the script above we set the environment vairable PATH to have the java installation path, so when the shell is started and when we type in java, it will look for the executables in the specified directory, if we want to change to a different jdk say java 7, just set the JAVA_HOME to a different directory.

However, this will not be working if the PATH contains other directorys that contains java executables, say there is symbolic link in /usr/bin/java pointing to a different jdk and we know by defualt, PATH will include /usr/bin as the default serach folder, so it will use the one in /usr/bin rather than the one in JAVA_HOME. We could use the second way to configure.

Configure Default Java to Run using update-alternatives

We now discuss the way to configure default java without changing PATH. We will use the tool update-algternatives, following these steps and remember we need sudo rights:

sudo update-alternatives --install "usr/bin/java" "java" "/path/to/jdk..." 1000
sudo update-alternatives --config java

There are 3 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
  ------------------------------------------------------------
  0            /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java   1071      auto mode
  1            /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java   1061      manual mode
  2            /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java   1071      manual mode
* 3            /path/to/jdk.../bin/java                         1000      manual mode

The update-alternateives tool updates the links in /etc/alternatives to point to the program. From the man page we can see update-alternatives creates, removes, maintains and displays information about the symbolic links comprising the Debian alternatives system. So we know from this.

The links in /etc/alternatives are just symbolic links. For example

ls -l /etc/alternatives


...
rwxrwxrwx 1 root root  ...  java -> /path/to/jdk1.8.0_xxx/bin/java
...

Moreover, the regular /usr/bin binaries are also symlinks. We can check it out by using ls too.

And the first command we use is:

--install link name path priority [--slave link name path]...
Add  a  group  of  alternatives to the system.

And the second command is the actual command to configure the default executables, we could see from the output, it will let us choose different alternatives, if there is no alternatives, we will see the following:

update-alternatives: error: no alternatives for XXX

Summary

So I have discussed how to configure default Java to run in Linux using startup scripts (e.g., .bash_profile) or the update-alternatives tool. It is a common problem when there are multiple versions of jdk to run, hopefully it helps.

Written on January 24, 2016