Config Default Java In Linux
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
- install java into say /path/to/jdk1.8_0_xx/
- edit your .bash_profile under your home diretory and add the following scripts
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:
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
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:
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:
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.