How To Set Alternative Java Path In Linux

Java is one of the most popular programming languages in the world and is widely used in a variety of environments. One of the key aspects of Java is its portability, which enables Java applications to run seamlessly on any platform that has a Java Runtime Environment (JRE) installed. For this reason, setting the Java path is a crucial step in various development and deployment processes. In this blog post, we will guide you on how to set an alternative Java path in Linux.

What is the Java Path?

In simple terms, the Java path is the system variable that your operating system uses to locate the Java binaries and libraries needed to run your Java applications. When you have multiple versions of Java installed on your system, you may need to set an alternative Java path.

Why Set an Alternative Java Path?

There could be several reasons for this. For instance, you may have an application that requires a specific version of Java. In this case, you will need to set the Java path to point to that particular version. Alternatively, you may want to test your application with different versions of Java. Again, you can do this by changing the Java path.

Setting the Alternative Java Path

Now, let’s get down to how you can set an alternative Java path in Linux:

Step 1: Install the Desired Java Version

If you don’t have your desired Java version installed on your system, install it first. You can do this using the package manager for your particular Linux distribution.

        sudo apt-get install openjdk-11-jdk

Replace “openjdk-11-jdk” with the package name of the Java version you want to install.

Step 2: Check the Installed Java Versions

Next, list all the installed Java versions on your system:

        sudo update-java-alternatives --list

Step 3: Set the Alternative Java Path

Now you can set your desired version as the default using the following command:

        sudo update-java-alternatives --set /path/to/java/version

Replace “path/to/java/version” with the path to the desired Java version that you got from the output of the previous command.

And that’s it! You’ve successfully set an alternative Java path in Linux. You can verify this by running the java -version command. It should now display the version you set as the default.


Setting an alternative Java path in Linux is essential when working with multiple versions of Java. It allows you to specify which version should be used by default. This blog post provided an easy-to-follow guide to setting an alternative Java path in Linux. Happy coding!