You will probably come across a situation where you have to work with different versions of Java; when Linux is your development platform, you can easily switch between them. For instance, if a project requires Java 17, you can switch to that. When your next project requires Java 18, you can quickly make the switch. Let me show you how.
First, we’re going to install Java 17 and Java 18, so you can see how the switching is done.
- To install Java 17, open a terminal window and issue this command:
sudo apt-get install openjdk-17-jdk openjdk-17-jre -y
- After that installation completes, let’s install Java 18 with this command:
sudo apt-get install openjdk-18-jdk -y
Now Java 17 and Java 18 are installed on your machine, ready to help you build your projects.
- To switch between Java versions, issue this command:
update-alternatives --config java
This will present you with a list of the different versions of Java you have installed. Each version will be associated with a number, starting with 0.
- If you want to switch to Java 18, locate and type the associated number for Java 18 and hit Enter on your keyboard. The system will set Java 18 as the current version. You can verify the switch with the following command, where you should see Java 18 listed:
And that’s all there is to switching between versions of Java on Linux. You can install as many versions as you like and switch between them for different projects on the fly.
Subscribe to TechRepublic’s How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.
Subscribe to the Developer Insider Newsletter
From the hottest programming languages to commentary on the Linux OS, get the developer and open source news and tips you need to know. Delivered Tuesdays and Thursdays