How to Install OpenJDK 11 on Ubuntu 22.04 LTS

Java is a general-purpose, class-based, object-oriented multipurpose programming language that is popular due to the design of having lesser implementation dependencies, meaning that the compiled Java code can be run on all platforms that support Java without the need for recompilation. Java is also fast, secure, and reliable, therefore. It is widely used for developing Java applications in laptops, data centers, game consoles, scientific supercomputers, cell phones, etc.

The tutorial will look at installing the OpenJDK version instead of the default Oracle JDK. The difference between these two is licensing. OpenJDK is an entirely free, open-source Java with a GNU General Public License, and Oracle JDK requires a commercial license under the Oracle Binary Code License Agreement. Other differences are release schedules and other factors that come into play; however, performance is the same.

In the following tutorial, you will learn how to install OpenJDK 11 LTS or better known as Java 11 LTS, on Ubuntu 22.04 LTS Jammy Jellyfish with the standard APT installation from Ubuntu’s repository along with the PPA version, which may suit some users better. The tutorial will also demonstrate how to switch Java alternative default versions.

Update Ubuntu

Update your system to ensure all existing packages are up to date to avoid any conflicts during the installation.

sudo apt update && sudo apt upgrade -y

Install OpenJDK 11 – APT Method with Ubuntu

The first and most recommended option is to install OpenJDK using the default Ubuntu 22.04 repository. First, search to find what is available.

In your terminal, use the following command.

apt-cache search openjdk-11 | grep openjdk-11

Example output:

How to Install OpenJDK 11 on Ubuntu 22.04 LTS

As the above output states, OpenJDK 11 JDE and JRE are available to install.

To begin the installation, use the following terminal command.

Install OpenJDK 11 LTS – JRE

sudo apt-get install openjdk-11-jre -y

Install OpenJDK 11 LTS – JRE

sudo apt-get install openjdk-11-jdk -y

Updates are handled with the standard apt update and upgrade commands. However, you can remove them separately or altogether if you no longer require JDK or JRE.

Example:

sudo apt-get autoremove openjdk-11-jre openjdk-11-jdk --purge

Note that this will remove any unrequited leftover dependencies and thoroughly wipe the installation and data as much as possible from your system.

Install OpenJDK 11 – APT Method with PPA

Ubuntu 22.04 repository often has the latest up-to-date and secure versions. An alternative method is to add the ppa:openjdk-r/ppa repository, which is always up to date and may see updates deployed quicker than Ubuntu’s default archives.

The PPA is untrusted, in the short term, has no affiliation with the Ubuntu core team. However, it has been around for a long time and is widely used in larger communities.

Note that you do not need to remove and default OpenJDK installations. You can install this PPA directly. If any updates are available, it will prompt you to update your version from its repository.

First, add the PPA using the following command.

sudo add-apt-repository ppa:openjdk-r/ppa -y

Next, you can install JDK or JRE.

Install OpenJDK 11 LTS – JRE

sudo apt-get install openjdk-11-jre -y

Install OpenJDK 11 LTS – JRE

sudo apt-get install openjdk-11-jdk -y

Users that already have the above installed just run an update to check if any newer versions are available.

sudo apt update && sudo apt upgrade

For users that no longer wish to use this PPA, you will need to remove the OpenJDK versions off your system first.

sudo apt-get remove openjdk-11-jre openjdk-11-jdk

Then remove the PPA with the –remove syntax for removal.

sudo add-apt-repository --remove ppa:openjdk-r/ppa -y

Switching Alternative Java Versions

Once you installed OpenJDK 11, you may notice an alternative version number when checking using the standard java –version command. Alternatively, you may install an alternative version that you would like to switch between. This can be done with the update-alternatives –config java command.

First, check what version is enabled. In the tutorial’s case, OpenJDK 11 was already the default-enabled option.

java --version

Example output:

How to Install OpenJDK 11 on Ubuntu 22.04 LTS

Next, list the alternative Java versions as follows.

sudo update-alternatives --config java

Example output:

How to Install OpenJDK 11 on Ubuntu 22.04 LTS

So as above, you see that Java 11 is enabled in the above example, but say you want to change to Java 18 (OpenJDK 18), you would choose the number 3 in the listing.

Example output:

How to Install OpenJDK 11 on Ubuntu 22.04 LTS

As above, OpenJDK 18 is enabled.

Once done, re-check the default version available using the following command.

java --version

Example output:

How to Install OpenJDK 11 on Ubuntu 22.04 LTS

To switch back to any other installed versions, use the same process.

Test Java – Create Hello World Application

It is always handy to test installations of these kinds to confirm everything is working correctly after being installed. The easy way is to create a small test using the famous Hello World example.

First, create the Java program file as follows.

sudo nano hello.java

Next, add the following Java code to the file below.

public class hello {
  public static void main(String[] args) {
    System.out.println("hi from LinuxCapable!");
  }
}

Save the file CTRL+O, then exit CTRL+X.

Next, compile the code using the following terminal command javac.

javac hello.java

You may still encounter issues compiling, delete the file and use the following instead with the –release 11 <filename>.java.

javac --release 11 hello.java

Finally, run the Java code with the following command.

java hello

Example output:

How to Install OpenJDK 11 on Ubuntu 22.04 LTS

Comments and Conclusion

In this tutorial, you have learned how to install Java 11 LTS on Ubuntu 22.04 LTS Jammy Jellyfish. You should now be able to run Java applications on your system that require Java 11 or, better yet, compile code.

For developers, ideally, you would like to start looking towards Java 17, which is an LTS version for your application development.



Follow LinuxCapable.com!

Like to get automatic updates? Follow us on one of our social media accounts!