selecting java version on ubuntu

vinicenter
2 min readMay 1, 2022

Intro

If you are using Ubuntu on your daily basis work, you probably have already faced a problem like, you need to run a .jar file but your Java JDK version is not compatible with that .jar file. So in this post I am going to show you how to download and select another version of Java JDK.

At first, you need to download the right version of Java JDK from APT

In my example, I am going to install openjdk-16-jdk with the following command:

$ sudo apt install openjdk-16-jdk

But remember, if you need another version of Java JDK, APT have other packages like:

  • openjdk-8-jdk
  • openjdk-11-jdk
  • openjdk-13-jdk
  • openjdk-16-jdk
  • openjdk-17-jdk

Selecting the right version

Now that you downloaded the right version, you need to select that version.

To do that, type the following command:

$ sudo update-alternatives --config java

After you typed this command, you will have a output like:

There are 5 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-13-openjdk-amd64/bin/java 1311 manual mode
3 /usr/lib/jvm/java-16-openjdk-amd64/bin/java 1611 manual mode
4 /usr/lib/jvm/java-17-openjdk-amd64/bin/java 1711 manual mode
5 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode
Press "enter" to keep the current choice[*], or type selection number:

Type the selection number that is in the “Selection” column, in my case I am going to type 3 to select the version 16 of Java JDK and press Enter.

That’s all you need to do. To make sure you are running the selected version you can run the following command:

$ java --version

In my case the output is:

openjdk 16.0.1 2021-04-20
OpenJDK Runtime Environment (build 16.0.1+9-Ubuntu-120.04)
OpenJDK 64-Bit Server VM (build 16.0.1+9-Ubuntu-120.04, mixed mode, sharing)

That’s all I wanted to show you guys.

Happy coding!

--

--