Like Us On Facebook

Follow Us On Twitter

Drop Down Menu

Thursday 14 February 2019

Switch Between Multiple Java Versions on Linux

Hi friends,

In my previous blog post - Install Java 11 and Java 8 on Linux, I shared the way to install Java 11/Java 8 on your Linux machine. Now many of the current applications do not support Java 11 yet, so you must need a way to quickly switch between different installations of Java on your Linux machine. Without further ado, let's get started.


Way 1:

If you actually followed my way of installing Java 8/ Java 11 on your machine, and did set Java 8 / Java 11 default by using either of these commands:

sudo apt install oracle-java8-set-default

sudo apt install oracle-java11-set-default

Simply install the other command and it will automatically set that version as default in your system.

Now if you have both the above packages installed and now you want to set either of them as default, simply purge the one you wish to set as default and reinstall it.

So if you want to set Java 8 as default, run these commands in your terminal in order:

sudo apt purge oracle-java8-set-default

sudo apt install oracle-java8-set-default


If you want to set Java 11 as default, run these commands in your terminal in order:

sudo apt purge oracle-java11-set-default

sudo apt install oracle-java11-set-default



Way 2:


Run the following command in terminal:

sudo update-alternatives --config java


You can select which version you want to set default and then press enter. 

You might need to repeat the same operation for other java programs as well like:

sudo update-alternatives --config java
sudo update-alternatives --config javac
sudo update-alternatives --config javaws
sudo update-alternatives --config javadoc
sudo update-alternatives --config jar



Way 3:

Run the following command in terminal:

sudo update-java-alternatives --list

This will show the list of Java installations on your machine. Now based on which version would you want to set as default, run either of the following commands:

sudo update-java-alternatives --set /path/to/java/8

sudo update-java-alternatives --set /path/to/java/11


That's it. No need to restart the machine for the changes to take effect. However you might need to change the Project Structure in your IDE to reflect the Java version used to compile the code. 

P.S. Verify if changes have taken place by running echo $JAVA_HOME command in terminal.

If you've set JAVA_HOME variable in your /etc/environment file then you'll need to change it to point it to correct Java installation directory. In this case, you'll either need to restart your system to let the changes reflect globally or if you're working in a shell, use source /etc/environment command to force the shell session to pick up fresh variables from the file.


Update:


I recently came across a weird issue that even though my /etc/environment file had Java 8 set as JAVA_HOME variable, doing this  echo $JAVA_HOME  in terminal still pointed to Java 11 directory. I checked and there were no mentions of JAVA_HOME variable in any of the .bashrc files. To fix this, I edited my ~/.bashrc file and added the following variables:

export JAVA_HOME="/usr/lib/jvm/java-8-oracle"
export JRE_HOME="/usr/lib/jvm/java-8-oracle/jre"
export J2SDKDIR="/usr/lib/jvm/java-8-oracle"
export J2REDIR="/usr/lib/jvm/java-8-oracle/jre"
export DERBY_HOME="/usr/lib/jvm/java-8-oracle/db"

Let me know via comments if you face any trouble. Adios till my next post!


Do you like this post? Please link back to this article by copying one of the codes below.

URL: HTML link code: Forum link code: