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 on major Linux distributions.

2. update-java-alternatives

update-java-alternatives is a tool that lets us manage different versions of Java on Debian and Ubuntu. It’s a part of the java-common package:

$ sudo apt install -y java-common

Once installed, let’s verify it:

$ whereis update-java-alternatives 
update-java-alternatives: /usr/sbin/update-java-alternatives /usr/share/man/man8/update-java-alternatives.8.gz

Next, we’ll use this command to list the available Java versions:

$ update-java-alternatives --list
java-1.17.0-openjdk-amd64      1711       /usr/lib/jvm/java-1.17.0-openjdk-amd64
java-1.21.0-openjdk-amd64      2111       /usr/lib/jvm/java-1.21.0-openjdk-amd64
java-1.8.0-openjdk-amd64       1081       /usr/lib/jvm/java-1.8.0-openjdk-amd64

Notably, there are three versions of Java on this system:

  • OpenJDK 8
  • OpenJDK 17
  • OpenJDK 21

However, it doesn’t specify which version we’re currently running. For that, we’ll need to invoke java:

$ java --version
openjdk 21.0.1 2023-10-17
OpenJDK Runtime Environment (build 21.0.1+12-Ubuntu-223.04)

Now, if we need to change the Java version, we can use –set we can use –set:

$ sudo update-java-alternatives --set java-1.17.0-openjdk-amd64

Let’s verify that it has changed the version successfully:

$ java --version
openjdk 17.0.9 2023-10-17
OpenJDK Runtime Environment (build 17.0.9+9-Ubuntu-123.04)

3. update-alternatives

update-alternatives is a tool that lets us manage symbolic links between different versions of the same program. By default, it’s pre-installed on most Linux distributions like Debian, Fedora, OpenSUSE, and CentOS.

We can enlist the available Java versions using the –config option:

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

  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-21-openjdk-amd64/bin/java      2111      auto mode
  1            /usr/lib/jvm/java-17-openjdk-amd64/bin/java      1711      manual mode
  2            /usr/lib/jvm/java-21-openjdk-amd64/bin/java      2111      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:

The config option expects the binary or the command name, which is java in this case.

In the list, the asterisk before the selection number signifies the default Java version. In other words, OpenJDK 21 is currently in use. At the bottom of the report, we can see a prompt that asks for the selection number, where we can switch to another version if we wish.

As an example, we’ll enter 1 to switch to OpenJDK 17. We should keep in mind that we’ll need to invoke the command as root to change the Java version.

Afterwards, we’ll enlist the versions again to verify that the JDK has changed successfully:

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

  Selection    Path                                            Priority   Status
------------------------------------------------------------
  0            /usr/lib/jvm/java-21-openjdk-amd64/bin/java      2111      auto mode
* 1            /usr/lib/jvm/java-17-openjdk-amd64/bin/java      1711      manual mode

...

4. archlinux-java

archlinux-java is specifically designed for Arch Linux and derivatives. It’s part of the java-runtime-common package, which we can install from the official package repositories:

$ sudo pacman -S --noconfirm java-runtime-common

Once installed, let’s enlist the available Java versions:

$ archlinux-java status
jdk11-openjdk    openjdk 11.0.16 11.0.16+9-1   default
jdk17-openjdk    openjdk 17.0.4  17.0.4+7-1

Similarly, we can switch to a different Java version using the set option:

$ sudo archlinux-java set jdk17-openjdk

Now, let’s verify that it has changed successfully:

$ archlinux-java status
jdk11-openjdk    openjdk 11.0.16 11.0.16+9-1
jdk17-openjdk   openjdk 17.0.4  17.0.4+7-1     default

5. galternatives

galternatives is a GUI front-end to update-alternatives. 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.

6. 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.

6.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.

6.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.

6.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.

6.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.

6.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.

7. Conclusion

In this article, we learned how to switch between Java versions. For that purpose, we made use of various utilities like update-java-alternativesupdate-alternatives, and archlinux-java. Next, we talked about galternatives, which is a GUI front-end to update-alternatives.

Finally, we looked at the cross-platform utility SDKMAN, which can assist in managing different Java versions on the same machine.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.