best cloud web hosting services

Mar 14, 2023

9 min read

How To Install Java and its Virtual Machine on Ubuntu 22.04

Written by

Abdelhadi Dyouri

Java is a high-level, object-oriented programming language. The JVM or Java’s virtual machine, is a virtual machine that enables a computer to run Java and programs that are written in a language that can be compiled to Java bytecode such as Scala and Kotlin programs. Java and JVM are required for many DevOps tools such as Jenkins, Cassandra, and Apache Tomcat.

In this tutorial, you will learn how to install the Java Runtime Environment (JRE) and the Java Developer Kit (JDK) using apt. The JDK provides software tools to develop Java programs. The JRE executes the programs you write in Java. In this tutorial, you will install OpenJDK, which is an open-source implementation of Java. However, you can also install the Oracle JDK, which is the original version. Both OpenJDK and OracleJDK are developed and maintained by Oracle, the developers of Java, and both are functionally identical since Java 11.

Prerequisites

  • Basic knowledge of the Linux command line.
  • An Ubuntu 22.04 server with a non-root user with sudo privileges. You can get affordable, and powerful Ubuntu servers from our website, and you can check out our How to access your server using SSH guide to learn how to access your server and create a sudo user.

Updating the Package Cache

Start by updating the packages in the package manager cache to the latest available versions using the following command:

sudo apt update

Installing the JRE/JDK using Apt

To install the JRE from the OpenJDK version of Java, use the following command:

sudo apt install default-jre

This will install the default JRE in the Ubuntu repositories, and will allow you to execute Java programs.

Once the installation is finished, check your Java version to ensure that the JRE was successfully installed:

java -version

You should receive an output similar to the following:

openjdk version "11.0.17" 2022-10-18
OpenJDK Runtime Environment (build 11.0.17+8-post-Ubuntu-1ubuntu222.04)
OpenJDK 64-Bit Server VM (build 11.0.17+8-post-Ubuntu-1ubuntu222.04, mixed mode, sharing)

Next, install the JDK to compile and run some Java software that requires it. To do so, use the following command:

sudo apt install default-jdk

To ensure that the JDK is successfully installed, check the version of the Java compiler javac:

javac -version

You should receive an output similar to the following:

javac 11.0.17

With this, you now have Java installed on your system with OpenJDK. If you are looking to install the Official Oracle JDK, follow the next option.

(Alternative Option) Installing Oracle JDK 11

If you want to install and use OracleJDK, and not OpenJDK, or switch between the two depending on your requirements, use the following instructions.

First, install some required packages:

sudo apt install -y libc6-x32 libc6-i386

Go to the Oracle downloads page and download the x64 Debian Package version using wget:

wget https://download.oracle.com/java/19/latest/jdk-19_linux-x64_bin.deb

Once your .deb package is downloaded, install it using apt:

sudo apt install ./jdk-19_linux-x64_bin.deb

Once installed, use the following commands to set these new packages for update-alternatives:

sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-19/bin/java 100
sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-19/bin/javac 100

Managing Java Alternatives

If you've installed one or more Java versions, then you can use the update-alternatives command to set the default version for your system.

To select your default Java installation, run the following command:

sudo update-alternatives --config java

You'll receive a list of the Java versions you currently have installed on your system:

There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                         Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-11-openjdk-amd64/bin/java   1111      auto mode
  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java   1111      manual mode
  2            /usr/lib/jvm/jdk-19/bin/java                  100       manual mode

Press <enter> to keep the current choice[*], or type selection number: 

Press Enter to leave the current option, or type the number of the Java version you want to set.

For example, type in 2 and press Enter to set the OracleJDK version.

You can also use update-alternatives to configure the default version of your Java compiler:

sudo update-alternatives --config javac

You should receive an output similar to the following:

There are 2 choices for the alternative javac (providing /usr/bin/javac).

  Selection    Path                                          Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-11-openjdk-amd64/bin/javac   1111      auto mode
  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/javac   1111      manual mode
  2            /usr/lib/jvm/jdk-19/bin/javac                  100       manual mode

Again, choose 2 to use the OracleJDK compiler as your default compiler and press Enter.

To verify that Java was installed, check out its version:

java -version

You should receive an output similar to the following:

java version "19.0.1" 2022-10-18
Java(TM) SE Runtime Environment (build 19.0.1+10-21)
Java HotSpot(TM) 64-Bit Server VM (build 19.0.1+10-21, mixed mode, sharing)

To verify that the Java compiler was installed, check out its version:

javac -version

You should receive an output similar to the following:

javac 19.0.1

Configuring the Java Home Directory Environment Variable

Now that you've installed Java on your Ubuntu server, you need to to set up the $JAVA_HOME environment variable, which is a variable used by many Java packages and software programs written in Java to determine where Java is installed.

First run the update-alternatives to get the path of your current Java installation:

sudo update-alternatives --config java

This will list paths of your different Java installations, which will be similar to the following:

  0            /usr/lib/jvm/java-11-openjdk-amd64/bin/java   1111      auto mode
  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java   1111      manual mode
  2            /usr/lib/jvm/jdk-19/bin/java                  100       manual mode

Here, OpenJDK is located at /usr/lib/jvm/java-11-openjdk-amd64/bin/java and OracleJDK is located at /usr/lib/jvm/jdk-19/bin/java.

Choose and copy a path depending on your requirements, then open the /etc/environment file:

sudo nano /etc/environment

Add your path at the end of this file and assign it to a variable called JAVA_HOME, excluding the /bin/java portion, like so:

JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"

Save and close /etc/environment.

Reload /ect/environment:

source /etc/environment

Now check out the value of your Java Home variable:

echo $JAVA_HOME

This should print the path you've set:

/usr/lib/jvm/java-11-openjdk-amd64

Congrats

You have now learned how to install Java on your Ubuntu system using two methods, one where you install the OpenJDK version available in your Ubuntu repositories, and one where you install the official OracleJDK. You've also learned how to install both versions and alternate between them as your requirements change.

A note about tutorials: We encourage our users to try out tutorials, but they aren't fully supported by our team—we can't always provide support when things go wrong. Be sure to check which OS and version it was tested with before you proceed.

If you want a fully managed experience, with dedicated support for any application you might want to run, contact us for more information.

Leave a Reply