How to Switch between Java Versions on Ubuntu Linux ?

If you have installed multiple JDK on ubuntu and sometimes you might need to change the version of installed JAVA to get few things working with one version and few other things working with another version of JAVA.

Lets first identify which all JDK are installed on your ubuntu, this can be done using “update-java-alternatives –list” command as,

$ update-java-alternatives --list
java-1.11.0-openjdk-amd64      1111       /usr/lib/jvm/java-1.11.0-openjdk-amd64
java-1.8.0-openjdk-amd64       1081       /usr/lib/jvm/java-1.8.0-openjdk-amd64

As we can see above, we have two JDK installed, java 1.11.0 and java 1.8.0 , now if we want to change the default Java, we can use below command . Note, you will need to enter the selection number as shown in the table to choose appropriate java

 $ sudo update-alternatives --config java
There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1111      auto mode
  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1111      manual mode
  2            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode

Press  to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java to provide /usr/bin/java (java) in manual mode

Now, we can verify the updated JAVA version as,

 $ /usr/bin/java -version
openjdk version "1.8.0_212"
OpenJDK Runtime Environment (build 1.8.0_212-8u212-b03-0ubuntu1.18.04.1-b03)
OpenJDK 64-Bit Server VM (build 25.212-b03, mixed mode)
Follow Lynxbee

Leave a Reply / Ask Question