How to install Java on Ubuntu 22.04
Install Java on Ubuntu 22.04
Objective
Java is one of the most famous programming languages in the world. Its wide adoption over the past years makes it an unavoidable language in the development world. To learn more about the full capabilities of the Java language refer to the official documentation.
In this tutorial you will learn how to install the Java Development Kit (JDK) on a Ubuntu 22.04 Linux distribution.
Requirements
This tutorial assumes that you have a VPS, bare metal server, or as in our case, an OVHcloud Compute Instance running Ubuntu 22.04. You should also have basic knowledge of the command line. If you need help setting up a Public Cloud instance with Ubuntu 22.04, follow the guide to use an OVHcloud Compute Instance.
Instructions
This tutorial uses OpenJDK. This is the open source version but there are many licensed versions provided by a variety of vendors (Oracle, Microsoft, AWS, …).
In this tutorial, you will install OpenJDK, use it, and learn how to switch between different installed versions.
At the time of writing this tutorial, the latest LTS release of Java was 17.x and the last GA release was 18.x.
Installation of the OpenJDK LTS version
To install Java on Ubuntu, use the apt-get command:
sudo apt-get update && apt-get -y install openjdk-17-jdk
Output:
$ sudo apt-get update && apt-get -y install openjdk-17-jdk Hit:1 http://security.ubuntu.com/ubuntu jammy-security InRelease Hit:2 http://nova.clouds.archive.ubuntu.com/ubuntu jammy InRelease Hit:3 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-updates InRelease Hit:4 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-backports InRelease Reading package lists... Done Building dependency tree... Done Reading state information... Done The following additional packages will be installed: adwaita-icon-theme alsa-topology-conf alsa-ucm-conf at-spi2-core ca-certificates-java dconf-gsettings-backend dconf-service fontconfig fontconfig-config fonts-dejavu-core fonts-dejavu-extra gsettings-desktop-schemas gtk-update-icon-cache hicolor-icon-theme humanity-icon-theme java-common libasound2 libasound2-data libatk-bridge2.0-0 libatk-wrapper-java libatk-wrapper-java-jni libatk1.0-0 libatk1.0-data libatspi2.0-0 libavahi-client3 libavahi-common-data libavahi-common3 libcairo-gobject2 libcairo2 libcups2 libdatrie1 libdconf1 libdeflate0 libdrm-amdgpu1 libdrm-intel1 libdrm-nouveau2 libdrm-radeon1 libfontconfig1 libfontenc1 libgail-common libgail18 libgdk-pixbuf-2.0-0 libgdk-pixbuf2.0-bin libgdk-pixbuf2.0-common libgif7 libgl1 libgl1-amber-dri libgl1-mesa-dri libglapi-mesa libglvnd0 libglx-mesa0 libglx0 libgraphite2-3 libgtk2.0-0 libgtk2.0-bin libgtk2.0-common libharfbuzz0b libice-dev libice6 libjbig0 libjpeg-turbo8 libjpeg8 liblcms2-2 libllvm13 libpango-1.0-0 libpangocairo-1.0-0 libpangoft2-1.0-0 libpciaccess0 libpcsclite1 libpixman-1-0 libpthread-stubs0-dev librsvg2-2 librsvg2-common libsm-dev libsm6 libthai-data libthai0 libtiff5 libvulkan1 libwayland-client0 libwebp7 libx11-dev libx11-xcb1 libxau-dev libxaw7 libxcb-dri2-0 libxcb-dri3-0 libxcb-glx0 libxcb-present0 libxcb-randr0 libxcb-render0 libxcb-shape0 libxcb-shm0 libxcb-sync1 libxcb-xfixes0 libxcb1-dev libxcomposite1 libxcursor1 libxdamage1 libxdmcp-dev libxfixes3 libxft2 libxi6 libxinerama1 libxkbfile1 libxmu6 libxpm4 libxrandr2 libxrender1 libxshmfence1 libxt-dev libxt6 libxtst6 libxv1 libxxf86dga1 libxxf86vm1 mesa-vulkan-drivers openjdk-17-jdk-headless openjdk-17-jre openjdk-17-jre-headless session-migration ubuntu-mono x11-common x11-utils x11proto-dev xorg-sgml-doctools xtrans-dev ... update-alternatives: using /usr/lib/jvm/java-17-openjdk-amd64/bin/jhsdb to provide /usr/bin/jhsdb (jhsdb) in auto mode Setting up libgtk2.0-0:amd64 (2.24.33-2ubuntu2) ... Setting up humanity-icon-theme (0.6.16) ... Setting up libgail18:amd64 (2.24.33-2ubuntu2) ... Setting up libgtk2.0-bin (2.24.33-2ubuntu2) ... Setting up libgail-common:amd64 (2.24.33-2ubuntu2) ... Setting up openjdk-17-jre:amd64 (17.0.3+7-0ubuntu0.22.04.1) ... Setting up ubuntu-mono (20.10-0ubuntu2) ... Setting up openjdk-17-jdk:amd64 (17.0.3+7-0ubuntu0.22.04.1) ... update-alternatives: using /usr/lib/jvm/java-17-openjdk-amd64/bin/jconsole to provide /usr/bin/jconsole (jconsole) in auto mode Processing triggers for ca-certificates (20211016) ... Updating certificates in /etc/ssl/certs... 0 added, 0 removed; done. Running hooks in /etc/ca-certificates/update.d... done. done. Processing triggers for libglib2.0-0:amd64 (2.72.1-1) ... Processing triggers for libc-bin (2.35-0ubuntu3) ... Processing triggers for man-db (2.10.2-1) ... Setting up at-spi2-core (2.44.0-3) ... Processing triggers for libgdk-pixbuf-2.0-0:amd64 (2.42.8+dfsg-1) ... Scanning processes... Scanning candidates... Scanning linux images... Restarting services... /etc/needrestart/restart.d/systemd-manager systemctl restart packagekit.service [email protected] ssh.service systemd-journald.service systemd-networkd.service systemd-resolved.service systemd-timesyncd.service systemd-udevd.service udisks2.service Service restarts being deferred: systemctl restart systemd-logind.service systemctl restart [email protected] No containers need to be restarted. No user sessions are running outdated binaries. No VM guests are running outdated hypervisor (qemu) binaries on this host.
Verify the java installation:
java -version
Output:
$ java -version openjdk version "17.0.3" 2022-04-19 OpenJDK Runtime Environment (build 17.0.3+7-Ubuntu-0ubuntu0.22.04.1) OpenJDK 64-Bit Server VM (build 17.0.3+7-Ubuntu-0ubuntu0.22.04.1, mixed mode, sharing)
A good practice is to set an environment variable JAVA_HOME with the path to the JDK installation.
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64/ echo $JAVA_HOME
Output:
$ export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64/ $ echo $JAVA_HOME /usr/lib/jvm/java-17-openjdk-amd64/ $
Manage two or more JDK installations
Imagine you have to use another JDK version for a specific project. Install the specific version of OpenJDK, as described above.
sudo apt-get -y install openjdk-18-jdk
Output:
$ sudo apt-get -y install openjdk-18-jdk Reading package lists... Done Building dependency tree... Done Reading state information... Done The following additional packages will be installed: openjdk-18-jdk-headless openjdk-18-jre openjdk-18-jre-headless Suggested packages: openjdk-18-demo openjdk-18-source visualvm libnss-mdns fonts-ipafont-gothic fonts-ipafont-mincho fonts-wqy-microhei | fonts-wqy-zenhei fonts-indic The following NEW packages will be installed: openjdk-18-jdk openjdk-18-jdk-headless openjdk-18-jre openjdk-18-jre-headless 0 upgraded, 4 newly installed, 0 to remove and 12 not upgraded. Need to get 268 MB of archives. After this operation, 421 MB of additional disk space will be used. Get:1 http://nova.clouds.archive.ubuntu.com/ubuntu jammy/universe amd64 openjdk-18-jre-headless amd64 18~36ea-1 [48.0 MB] Get:2 http://nova.clouds.archive.ubuntu.com/ubuntu jammy/universe amd64 openjdk-18-jre amd64 18~36ea-1 [180 kB] Get:3 http://nova.clouds.archive.ubuntu.com/ubuntu jammy/universe amd64 openjdk-18-jdk-headless amd64 18~36ea-1 [220 MB] Get:4 http://nova.clouds.archive.ubuntu.com/ubuntu jammy/universe amd64 openjdk-18-jdk amd64 18~36ea-1 [10.9 kB] Fetched 268 MB in 5s (58.3 MB/s) .. update-alternatives: using /usr/lib/jvm/java-18-openjdk-amd64/bin/jhsdb to provide /usr/bin/jhsdb (jhsdb) in auto mode Setting up openjdk-18-jdk:amd64 (18~36ea-1) ... update-alternatives: using /usr/lib/jvm/java-18-openjdk-amd64/bin/jconsole to provide /usr/bin/jconsole (jconsole) in auto mode Processing triggers for hicolor-icon-theme (0.17-2) ... Scanning processes... Scanning candidates... Scanning linux images... Restarting services... Service restarts being deferred: systemctl restart systemd-logind.service systemctl restart [email protected] No containers need to be restarted. No user sessions are running outdated binaries. No VM guests are running outdated hypervisor (qemu) binaries on this host.
Verify the new installation:
java -version
Output:
$ java -version openjdk version "18-ea" 2022-03-22 OpenJDK Runtime Environment (build 18-ea+36-Ubuntu-1) OpenJDK 64-Bit Server VM (build 18-ea+36-Ubuntu-1, mixed mode, sharing)
Then set the JAVA_HOME environment variable:
export JAVA_HOME=/usr/lib/jvm/java-18-openjdk-amd64/ echo $JAVA_HOME
Output:
$ export JAVA_HOME=/usr/lib/jvm/java-18-openjdk-amd64/ $ echo $JAVA_HOME /usr/lib/jvm/java-18-openjdk-amd64/ $
It’s not very convenient to switch back to version 17. Fortunately, a tool exists–update-alternatives.
sudo update-alternatives --config java
Output:
$ sudo update-alternatives --config java There are 2 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/lib/jvm/java-18-openjdk-amd64/bin/java 1811 auto mode 1 /usr/lib/jvm/java-17-openjdk-amd64/bin/java 1711 manual mode 2 /usr/lib/jvm/java-18-openjdk-amd64/bin/java 1811 manual mode Press to keep the current choice[*], or type selection number: 1 update-alternatives: using /usr/lib/jvm/java-17-openjdk-amd64/bin/java to provide /usr/bin/java (java) in manual mode
Verify that the new java version is selected.
java -version
Output:
$ java -version openjdk version "17.0.3" 2022-04-19 OpenJDK Runtime Environment (build 17.0.3+7-Ubuntu-0ubuntu0.22.04.1) OpenJDK 64-Bit Server VM (build 17.0.3+7-Ubuntu-0ubuntu0.22.04.1, mixed mode, sharing)
⚠️ Warning:⚠️ if you have set the JAVA_HOME environment variable you have to update it with the rigth path.
Test the JDK installation
To test your Java installation by creating a Hello World application. Create a HelloWorld.java file and paste the following code into the file:
public class HelloWorld { public static void main(String[] args) { System.out.println("👋 Hello World!"); } }
Save and compile the file, then run it.
javac HelloWorld.java java HelloWorld
Output:
$ javac HelloWorld.java $ java HelloWorld.java 👋 Hello World!
That’s it, you have successfully installed and configured OpenJDK on Ubuntu 22.04.