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 check what is the current Java Version by using “java -version” command.
$ java -version
openjdk version "11.0.15" 2022-04-19
OpenJDK Runtime Environment (build 11.0.15+10-Ubuntu-0ubuntu0.21.10.1)
OpenJDK 64-Bit Server VM (build 11.0.15+10-Ubuntu-0ubuntu0.21.10.1, mixed mode, sharing)
As we can see, the current Java version is 11.0.15.
Now, Lets 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 and from version we have seen, currently we are running with Java 11, so if we want to switch between the versions, we can use the command “sudo update-alternatives –config java”
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 <enter> 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)