Log In

Switching between Java Versions on Ubuntu

Switching between Java Versions on Ubuntu
Hostman Team
Technical writer

Managing multiple Java versions on Ubuntu is essential for developers working on diverse projects. Different applications often require different versions of the Java Development Kit (JDK) or Java Runtime Environment (JRE), making it crucial to switch between these versions efficiently. Ubuntu provides powerful tools to handle this, and one of the most effective methods is using the update-java-alternatives command.

In this article, the process of switching between Java versions using updata-java-alternatives will be shown. This specialized tool simplifies the management of Java environments by updating all associated commands (such as java, javac, javaws, etc.) in one go. 

Overview of Java version management

Version control of Java is an essential part of development, particularly when dealing with numerous projects that have varying Java Runtime Environment (JRE) or Java Development Kit (JDK) requirements. Proper management guarantees that the appropriate Java version is used for every project, avoiding compatibility issues and ensuring smooth development workflows.

Importance of using specific Java versions

  • You must check that the Java version to be used is compatible with the application, program, or software running on the system. Using the appropriate Java version ensures that the product runs smoothly and without any compatibility issues.

  • Newer versions of Java usually come with updates and security fixes, which helps protect the system from vulnerabilities. Using an out-of-date Java version may expose the system to security vulnerabilities.

  • Performance enhancements and optimizations are introduced with every Java version. For maximum performance, use a Java version that is specific to the application.

Checking the current Java version

It is important to know which versions are installed on the system before switching to other Java versions.  To check the current Java version, the java-common package has to be installed. This package contains common tools for the Java runtimes including the update-java-alternatives method. This method allows you to list the installed Java versions and facilitates switching between them. Use the following command to install the java-common package:

sudo apt-get install java-common

E48e2dcc 0216 4bc8 9c75 100902862b9e

Upon completing the installation, verify all installed Java versions on the system using the command provided below:

sudo update-java-alternatives --list

5a64bd65 0092 463a 97db 5c0873fae320

Java versions 8 and 11 are installed on the system, as indicated by the output above. To find out which version is currently in used, execute the command below.

java -version


The displayed output indicates that the currently active version is Java version 11.

Installing multiple Java versions

From a technical perspective, on Ubuntu, the administrator can install as many Java versions as they want as long as there is enough disk space and the package repositories support them.

Follow the instructions below for installing multiple Java versions.

Begin by updating the system using the following command:  

sudo apt-get update -y && sudo apt-get upgrade -y


To add another version of Java, run the command below.

sudo apt-get install <java version package name>

In this example, installing Java version 17 can be done by running: 

sudo apt-get install openjdk-17-jdk openjdk-17-jre


Upon completing the installation, use the following command to confirm the correct and successful installation of the Java version:

sudo update-java-alternatives --list


Switching and setting the default Java version

To switch between Java versions and set a default version on Ubuntu Linux, you can use the update-java-alternatives command. 

sudo update-java-alternatives --set <java_version>

In this case, the Java version 17 will be set as default:

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


To check if Java version 17 is the default version, run the command: 

java -version


The output shows that the default version of Java is version 17.


In conclusion, managing multiple Java versions on Ubuntu Linux using update-java-alternatives is a simple yet effective process. By following the steps outlined in this article, users can seamlessly switch between different Java environments, ensuring compatibility with various projects and taking advantage of the latest features and optimizations offered by different Java versions. Because Java version management is flexible, developers may design reliable and effective Java apps without sacrificing system performance or stability.