In my day to day as Mulesoft Architect, I need to work with the latest version of the Mule runtime (to keep up with all new features) and with older versions (to provide support to old customers).
This sometimes requires to have different versions of the Java JDK. How do we deal with that? Do we need to have multiple servers/laptops to work with different JDK versions?
Fortunately, the answer is no, we don’t. We can have multiple versions of the Java JDK running on our system. In this post, we will see how to manage them.
Now, if we run the java —version command:
Prerequisites
- To follow this tutorial, we will use Ubuntu Server 24.04 LTS
- We will be using APT package manager. Before you install anything make sure you have updated the repositories on your Ubuntu by running:
sudo apt-get update
Install multiple versions of the OpenJDK
Let’s start from scratch, and let’s install 3 different versions of the OpenJDK:- OpenJDK 17
- OpenJDK 11
- OpenJDK 8
OpenJDK 17
sudo apt-get install openjdk-17-jdk
OpenJDK 11
sudo apt-get install openjdk-11-jdk
OpenJDK 8
sudo apt-get install openjdk-8-jdk
We can see that, apparently, our system is only using the OpenJDK 17 version. What happened to the other two versions? How can we use a different version?
Configuring the Default Java Version
Ubuntu Server allows us to have multiple Java installations and to define which one to use as default. If we have several installed, we can see all our JDK versions and set the default version with the update-alternatives command:
sudo update-alternatives --config java
As we can see, Ubuntu Server detects there are three different versions and it’s showing us that, by default, it will be using OpenJDK 17 version.
Let’s change it to OpenJDK 11, for example. In our case, we’ll be entering the option number 1. Now if we run again java —version we will see that now the default option is OpenJDK 11.
Let’s change it to OpenJDK 11, for example. In our case, we’ll be entering the option number 1. Now if we run again java —version we will see that now the default option is OpenJDK 11.
What about the JAVA_HOME env variable?
If your installation of Java requires the JAVA_HOME you will have to manually set/modify the value of the JAVA_HOME and point it to the right path of your preferred OpenJDK version. Check out our previous post to see how to set up the JAVA_HOME env variable.To switch between versions for your JAVA_HOME, here’s a trick you can do using some aliases:
Define an alias for each JDK version like this:
alias java-8="export JAVA_HOME='/usr/lib/jvm/java-8-openjdk-amd64'"
alias java-11="export JAVA_HOME='/usr/lib/jvm/java-11-openjdk-amd64'"
alias java-17="export JAVA_HOME='/usr/lib/jvm/java-17-openjdk-amd64'"
If you want to make these aliases permanent and be available to each terminal session, we just need to add them to the ~/.profile file. Edit the file with the command:
and add the previous lines with the aliases. Save the file and open a new SSH session to verify your aliases still work.
vi ~/.profile