1. Overview

Java is a popular programming language. It’s used by developers for creating a wide range of applications in different areas. Sometimes, we might need to keep multiple Java versions on the same system at the same time. For instance, some older Java applications might not work on newer Java versions and vice-versa. Also, we might want to test and develop Java apps that work on multiple Java versions.

In this tutorial, we’ll see how to switch between multiple Java versions.

We tested the commands in this tutorial on a Ubuntu 20.04 system. However, it should work on other Linux systems as well.

2. How to List Available Java Versions

Before we look for ways to switch between them, we’ll check which Java versions are available on our Linux system.

To get the current Java versions, let’s use the update-java-alternatives command, part of the java-common package included in the OpenJDK installation.

To illustrate, we’ll use update-java-alternatives with its –list option to see what versions of Java we have:

$ update-java-alternatives --list
java-1.11.0...     1111       /usr/lib/jvm/java-1.11...
java-1.8.0...      1081       /usr/lib/jvm/java-1.8...

Notably, there are two versions of Java on our system:

  • OpenJDK 11
  • OpenJDK 8

Further, we can use the more general update-alternatives command to show each Java version with its priority:

$ update-alternatives --config java
There are 2 choices for the alternative java   ...
  Selection    Path                            ...
  0            /usr/.../java-11.../bin/java    ...
* 1            /usr/.../java-11.../bin/java    ...
  2            /usr/.../java-8.../jre/bin/java ...
Press <enter> to keep the current choice[*], or type selection number:

Here, the * asterisk in the Selection field shows the Java version that is now used as default. This command brings up a prompt where we can choose which version of Java to use. Since all we want to do now is to get the list above, we just press Enter to leave the prompt.

3. How to Switch Java Versions

As we already saw, OpenJDK 8 and 11 are installed on our system. Before making a switch between these, let’s see the current one with the java command:

$ java -version
openjdk 11.0...

Right now, OpenJDK 11 is the default version. Let’s see how to switch this default to OpenJDK 8.

3.1. Select the Default Java Version

We can set the default Java version by using the update-java-alternatives command with its –set flag:

$ sudo update-java-alternatives --set /usr/lib/jvm/java-1.8.0-openjdk-amd64

Here, we set OpenJDK 8 as the default version by specifying the respective path.

Let’s verify the switch via java:

$ java -version
openjdk version 1.8...

Clearly, the Java version is now set to OpenJDK 8.

3.2. Switch Java Version Interactively

We can also use the update-alternatives command to choose the default Java version interactively with the –config java switch:

$ sudo update-alternatives --config java
There are 2 choices ...
Selection    Path                         ...
  0   /usr/.../java-11-openjdk.../bin/java...
* 1   /usr/.../java-11-openjdk.../bin/java ...
  2   /usr/.../java-8-openjdk.../jre/bin/java...
Press <enter> to keep the current choice[*]...

This shows a list of installed Java versions along with their paths. The sudo command ensures we have enough permissions for administrative work.

We can choose the one we want to use as the default by using its corresponding number. For instance, we choose 1 to set the Java version to OpenJDK 11.

Let’s now see if we were able to switch to the new Java version:

$ java -version
openjdk version 11.0...

We’re back to 11 as the default.

3.3. Switch Java Version Using galternatives

Basically, galternatives is update-alternatives in graphical mode. It enables users to choose a particular application to run for common commands.

Let’s see how to use galternatives to choose a Java version.

First, we start galternatives from either the terminal or the application menu:

$ sudo galternatives

Under the Groups column in the main window, we can see a list of programs. We can look for or add java in the list.

For instance, on our system, we see two Java versions:

Galternatives Main Window

Notably, OpenJDK 8 is the default version now.

Let’s change the Java version to OpenJDK 11 by using the radio buttons:

Galternatives changing Java version.

We can also verify the changes in the terminal:

$ java -version
openjdk version 11.0....

Consequently, we changed the Java version from 8 to 11.

4. Using SDKMAN

SDKMAN or Software Development Kit Manager is a tool that helps to manage multiple Software Development Kits (SDKs). However, SDKMAN isn’t exclusive to SDKs. It can manage other tools and packages as well. For example, it can manage build tools like Gradle, Ant, etc.

4.1. Installing SDKMAN

SDKMAN can be installed on multiple operating systems. Let’s install it on Ubuntu 22.04.

First, we retrieve the SDKMAN installation script using curl. Then, we use the pipe operator to redirect the output to the bash command:

$ curl -s "https://get.sdkman.io" | bash

We then follow the on-screen procedure. Finally, we use the source command to read and execute the sdkman-init.sh script:

$ source ".sdkman/bin/sdkman-init.sh"

Also, to confirm the installation, we can try using the sdk command to check its version:

$ sdk version
SDKMAN!
script: 5.18.2
native: 0.4.2

The above command shows the script and native versions. Particularly, the sdk command manages all SDKMAN functions that we’ll use.

4.2. Listing Available JDKs

Once installed, SDKMAN can list all the available SDK candidates:

$ sdk list

Furthermore, we can list the available versions of a specific candidate, say JDK:

$ sdk list java
========================================================================
Available Java Versions for Linux 64bit
========================================================================
 Vendor        | Use | Version      | Dist    | Status     | Identifier
------------------------------------------------------------------------
 Corretto      |     | 21           | amzn    |            | 21-amzn    
               |     | 20.0.2       | amzn    |            | 20.0.2-amzn
...

From the list above, we can install a specific Java version using its identifier.

4.3. Installing JDK With SDKMAN

Let’s install versions 17.0.7 and 17.0.8 of the Java SDK by JetBrains:

$ sdk install java 17.0.7-jbr

$ sdk install java 17.0.8-jbr

Notably, SDKMAN can list the Java versions it installed. However, it cannot list those installed by other means.

Let’s check the current version of Java:

$ java -version
openjdk version "17.0.8" 2023-07-18
OpenJDK Runtime Environment JBR-17.0.8+7...

In this case, the current version of Java is 17.0.8.

4.4. Switching Java Versions

To switch Java versions, we utilize the use subcommand of SDKMAN. For example, let’s switch to version 17.0.7-jbr:

$ sdk use java 17.0.7-jbr
Using java version 17.0.7-jbr in this shell.

Let’s make the changes permanent by using the default subcommand:

$ sdk default java 17.0.7-jbr
setting java 17.0.7-jbr as the default version for all shells.

To check the new default Java version, we again run the version command:

$ java -version
openjdk version "17.0.7" 2023-04-18
OpenJDK Runtime Environment JBR-17.0.7...
...

As a result, the Java version is changed successfully.

4.5. Comparison of SDKMAN With Other Methods

Notably, SDKMAN may not be compatible with all other methods for managing Java versions. For example, if we’re using a package manager to manage Java versions, we may not be able to use SDKMAN to switch between Java versions.

Also, SDKMAN may be convenient and easy to use for beginners. However, if we need more control over the installation process or if we’re using a package manager or IDE to manage Java versions, we may want to use a different method.

Furthermore, SDKMAN has multiple SDKs available to choose from and can easily integrate with IDEs, such as IntelliJ IDEA and Eclipse. However, SDKMAN is limited to Java-specific tools.

5. Conclusion

In this article, we learned how to switch the current Java version.

First, we looked at how to list all of the Java installations on our system. Second, we used the update-java-alternatives tool to set the default Java version. Then, we changed to a different version of Java by using the update-alternative command. Next, we talked about galternatives, which is like the update-alternatives tool but with a graphical interface. Finally, we saw how SDKMAN can help manage different Java versions.

Comments are closed on this article!