How To Work With Multiple Versions Of Java On Linux
Malcom Ridgers is a tech expert at BairesDev specializing in the software outsourcing industry.
Java has been a developer staple for quite some time. In fact, Java is one of the most popular programming languages on the market. Part of the reason for that is Android.
When Java was first created, it was done so with the network in mind. Java also had the backing of major players in the industry (such as Sun Microsystems and IBM).
But Java has stood the test of time, and will continue to do so for the foreseeable future. And as Java continues to remain relevant, developers will continue to depend on this "write once, run anywhere" language.
That doesn’t mean Java isn't without its issues. One such caveat is that multiple versions of the language are still in use. For example, although Java 11 is available, many programs still depend on Java 8.
Why is this the case? Java 8 is a Long Term Support release, which means it'll be supported until 2030. Java 11 is also an LTS release, which enjoys support until 2026. The latest version, Java 14 was released in March 2020 but it isn’t an LTS.
So at the moment, there are three viable Java releases developers (and users) have to contend with.
For many developers (such as those who work with Java development services), how do you deal with multiple releases of Java? Let's take a look at how this is done on Linux.
Installing Java on Linux
Let's say you want to install both JDK (Java Developer Kit) 8 and 11 on Ubuntu Linux. You can easily do this from the command line.
Open a terminal window and install Java 8 with the command:
sudo apt-get install openjdk-8-jre-headless -y
Install Java 11 with the command:
sudo apt-get install openjdk-11-jre-headless -y
Choosing which Java to use
The problem with this is that, when you install Java 11, it will cause your system to automatically default to Java 11. What happens when you need to switch back to using Java 8? Fortunately, there's a tool that allows you to select your default. That tool is update-alternatives, and it allows you to configure a number of tools when you have multiple versions installed.
In the case of Java, when you have both Java 8 and 11 installed, you would run the command:
sudo update-alternatives --config java
You will be presented with the available options (in this case Java 8 and 11). To select which you want to set as the option to use on your system, type the number associated with the version to be used (Figure 1), and hit Enter.
Selecting between Java 8 and 11 on Ubuntu Linux.
Anytime you need to switch Java versions, run that same command and select the version you want to use.
How to set the JAVA_HOME environment variable
A number of Java applications depend on the JAVA_HOME environment variable to set the location of the Java installation. To determine where is this location, you'd first have to re-run the update-alternatives command like so:
sudo update-alternatives --config java
The output of that command not only allows you to select the version of Java you want to use, but it also includes the path for that version. Take a look at the example above (in Figure 1). You'll see that:
The Java 11 path is:
The Java 8 path is:
Copy the path for the Java version you want to use and then issue the command:
sudo nano /etc/environment
At the bottom of the file, add the environment like so:
Where PATH is the path of the version you want to use for the JAVA_HOME environment variable. In our example, for Java 11 that line would be:
For Java 8, that line would be:
Once you've made the changes, save and close the file. Reload the environment file with the command:
Finally, verify the path has changed with the command:
The output of the command should match the path of the Java version you chose to use.
It doesn't matter how many versions of Java you have installed on Linux. With just a bit of care, your Java development can be made significantly easier. Working with multiple versions of Java doesn't have to be a challenge. With a couple of easy commands, you can take control of your environment.
Subscribe to get your daily round-up of top tech stories!