How to install multiple versions of Java on Ubuntu 20.04 — Beginners Guide

Hasith Magage
Javarevisited
Published in
2 min readAug 21, 2021

--

Here I’ll explain a few simple steps to install multiple versions of Java on your latest Ubuntu desktop.

In this example I’ll be using Oracle JDKs. You can download your favorite Oracle JDK from the following website,

Personally I’m using Java 7, 8 and 11. In java 11, you can directly download the Debian package and from the Oracle official website and install it using the following command.

sudo dpkg -i path_to_deb_file

But, for the older versions of java, you need to download the gzip file and install them manually.

As an example I’m writing down the commands for JDK 7 here. You can repeat these commands for your tar.gz file of your preferred version.

Extract using the tar command. In here you can see few options given for extracting,

  • x — Extract the archive
  • v — Displays Verbose Information
  • z —Used to extract zip files zipped using gzip algorithm.
  • f — Creates archive with given filename
tar xvzf jdk-7u80-linux-x64.tar.gz

Change the owner of the dir to root, by default the owner should be you.

sudo chown -R root:root jdk1.7.0_80

Move this into /usr/lib/jvm

sudo mv jdk1.7.0_80 /usr/lib/jvm

Install java (Configure what are the alternative Java versions available in your computer)

  • 1065 is the priority, higher the number, higher the priority
sudo update-alternatives — install /usr/bin/java java /usr/lib/jvm/jdk1.7.0_80/bin/java 1065sudo update-alternatives — install /usr/bin/javac javac /usr/lib/jvm/jdk1.7.0_80/bin/javac 1065sudo update-alternatives — install /usr/bin/jar jar /usr/lib/jvm/jdk1.7.0_80/bin/jar 1065sudo update-alternatives — install /usr/bin/javaws javaws /usr/lib/jvm/jdk1.7.0_80/bin/javaws 1065

Switch the default version of java as you need. (The highest priority version that you installed should be set default automatically as you install)

  • java — Java application launcher
  • javac — Java compiler
  • jar — Java archive tool
  • javaws — Launch java applications from the browser. Java Web Start is deprecated from Java 11 onwards.
sudo update-alternatives — config javasudo update-alternatives — config javacsudo update-alternatives — config jarsudo update-alternatives — config javaws

Instead of $JAVA_HOME, alternatives does allow you to have certain tools use one installation of Java while other tools use a completely different one.

But the need of setting $JAVA_HOME completely depends on the use case.

Happy coding!

--

--

Hasith Magage
Javarevisited

Experienced Software Engineer | BEng (Hons) | OCPJP