It is common for us to have multiple versions of Java/JDK in our Ubuntu Linux namely Java 8, 11 or 17…but how can we change the default Java/JDK version? We can use the update-alternatives command inside the Ubuntu.
First thing first, first we must make sure we have multiple versions of JDK inside our Ubuntu machine, we can check using the command below:
apt list --installed *jdk*
Alternatively, you can use update-alternatives command too to list out all Java inside your machine.
update-alternatives --list java
After confirmed we have multiple versions of Java, we can run the update-alternatives --config command. For e.g. to update Java version, we use the command below:
sudo update-alternatives --config java
It will list out options for you to choose like below. The * beside 1 mean current choice, and you have 0 to 3 options to choose. For this example, 0 had been selected. The update-alternatives will then update the symbolic links determining default Java command.
There are 3 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status ------------------------------------------------------------ 0 /usr/lib/jvm/java-17-openjdk-amd64/bin/java 1711 auto mode * 1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 manual mode 2 /usr/lib/jvm/java-17-openjdk-amd64/bin/java 1711 manual mode 3 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode Press <enter> to keep the current choice[*], or type selection number: 0 update-alternatives: using /usr/lib/jvm/java-17-openjdk-amd64/bin/java to provide /usr/bin/java (java) in auto mode
Please note the command above only update java command. You still have other commands that need to update accordingly. You can find most of the commands in /usr/lib/jvm/java-17-openjdk-amd64/bin/ folder. For e.g. in this scenario, the folder has the command below:
ls /usr/lib/jvm/java-17-openjdk-amd64/bin/ jar javac jcmd jdeprscan jhsdb jlink jpackage jshell jstatd serialver jarsigner javadoc jconsole jdeps jimage jmap jps jstack keytool java javap jdb jfr jinfo jmod jrunscript jstat rmiregistry
You might do not need to update them all since it depends on your usage, but it is quite common to update java, jar, javac, javadoc. The commands will be like below:
sudo update-alternatives --config jar sudo update-alternatives --config javac sudo update-alternatives --config javadoc






