Install Java 11|Java 17|Java 18 on Ubuntu 22.04

1
371

Follow through this tutorial to quickly learn how to install Java 11|Java 17|Java 18 on Ubuntu 22.04.

Learning Java Programming - video 1...
Learning Java Programming - video 1- "Setting up Java on your device"

Install Java 11|Java 17|Java 18 on Ubuntu 22.04

There are two versions of Java; OpenJDK and Oracle JDK;

While both are products of Oracle, OpenJDK is an opensource version while Oracle JDK is a enterprise product that requires a commercial license to use.

Install OpenJDK 11|OpenJDK 17|OpenJDK 18 on Ubuntu 22.04

OpenJDK 11|OpenJDK 17|OpenJDK are available on the default Ubuntu 22.04 Universe repositories.

apt-cache search openjdk | egrep '11|17|18'

Sample output;

openjdk-11-dbg - Java runtime based on OpenJDK (debugging symbols)
openjdk-11-doc - OpenJDK Development Kit (JDK) documentation
openjdk-11-jdk - OpenJDK Development Kit (JDK)
openjdk-11-jdk-headless - OpenJDK Development Kit (JDK) (headless)
openjdk-11-jre - OpenJDK Java runtime, using Hotspot JIT
openjdk-11-jre-headless - OpenJDK Java runtime, using Hotspot JIT (headless)
openjdk-11-source - OpenJDK Development Kit (JDK) source files
libjax-maven-plugin - Using the xjc goal with OpenJDK 11+
openjdk-11-demo - Java runtime based on OpenJDK (demos and examples)
openjdk-11-jre-dcevm - Alternative VM for OpenJDK 11 with enhanced class redefinition
openjdk-11-jre-zero - Alternative JVM for OpenJDK, using Zero
openjdk-17-dbg - Java runtime based on OpenJDK (debugging symbols)
openjdk-17-demo - Java runtime based on OpenJDK (demos and examples)
openjdk-17-doc - OpenJDK Development Kit (JDK) documentation
openjdk-17-jdk - OpenJDK Development Kit (JDK)
openjdk-17-jdk-headless - OpenJDK Development Kit (JDK) (headless)
openjdk-17-jre - OpenJDK Java runtime, using Hotspot JIT
openjdk-17-jre-headless - OpenJDK Java runtime, using Hotspot JIT (headless)
openjdk-17-jre-zero - Alternative JVM for OpenJDK, using Zero
openjdk-17-source - OpenJDK Development Kit (JDK) source files
openjdk-18-dbg - Java runtime based on OpenJDK (debugging symbols)
openjdk-18-demo - Java runtime based on OpenJDK (demos and examples)
openjdk-18-doc - OpenJDK Development Kit (JDK) documentation
openjdk-18-jdk - OpenJDK Development Kit (JDK)
openjdk-18-jdk-headless - OpenJDK Development Kit (JDK) (headless)
openjdk-18-jre - OpenJDK Java runtime, using Hotspot JIT
openjdk-18-jre-headless - OpenJDK Java runtime, using Hotspot JIT (headless)
openjdk-18-jre-zero - Alternative JVM for OpenJDK, using Zero
openjdk-18-source - OpenJDK Development Kit (JDK) source files
uwsgi-plugin-jvm-openjdk-11 - Java plugin for uWSGI (OpenJDK 11)
uwsgi-plugin-jwsgi-openjdk-11 - JWSGI plugin for uWSGI (OpenJDK 11)
uwsgi-plugin-ring-openjdk-11 - Closure/Ring plugin for uWSGI (OpenJDK 11)
uwsgi-plugin-servlet-openjdk-11 - JWSGI plugin for uWSGI (OpenJDK 11)

If you want, you can install just JRE or JDK.

“JDK is a superset of JRE, and contains everything that is in JRE, plus tools such as the compilers and debuggers necessary for developing applets and applications. JRE provides the libraries, the Java Virtual Machine (JVM), and other components to run applets and applications written in the Java programming language”.

We will install OpenJDK in this tutorial;

To install OpenJDK 11 on Ubuntu 22.04, execute the commands below;

apt update
apt install openjdk-11-jdk

Confirm the version;

java --version
openjdk 11.0.15 2022-04-19
OpenJDK Runtime Environment (build 11.0.15+10-Ubuntu-0ubuntu0.22.04.1)
OpenJDK 64-Bit Server VM (build 11.0.15+10-Ubuntu-0ubuntu0.22.04.1, mixed mode, sharing)

To install OpenJDK 17 on Ubuntu 22.04, execute the commands below;

apt update
apt install openjdk-17-jdk

Confirm the version;

java --version
openjdk 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)

To install OpenJDK 18 on Ubuntu 22.04, execute the commands below;

apt update
apt install openjdk-18-jdk

Confirm the version;

java --version
openjdk 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)

How to Set Default Java Version on Ubuntu 22.04

If you have multiple versions of Java installed and want to set either Java 11|Java 17|Java 18 as the default, run the command below;

sudo update-alternatives --config java

Sample output;

There are 3 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-11-openjdk-amd64/bin/java   1111      manual mode
  2            /usr/lib/jvm/java-17-openjdk-amd64/bin/java   1711      manual mode
  3            /usr/lib/jvm/java-18-openjdk-amd64/bin/java   1811      manual mode

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

From the output above, you can see that Java 18 is the current default Java version set.

For example, I would enter number 2 if i want to set Java 17 as the default version.

There are 3 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-11-openjdk-amd64/bin/java   1111      manual mode
  2            /usr/lib/jvm/java-17-openjdk-amd64/bin/java   1711      manual mode
  3            /usr/lib/jvm/java-18-openjdk-amd64/bin/java   1811      manual mode

Press  to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/lib/jvm/java-17-openjdk-amd64/bin/java to provide /usr/bin/java (java) in manual mode

To set another default version, rerun the sudo update-alternatives --config java command choose the enter the number of your selection and press ENTER from the above command output.

Set JAVA HOME Environment Variable

Next you can set Java home environment variable of your Java selection by updating the path to the Java version.

For example, current version of Java selected;

java --version
openjdk 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)

The path can be obtained by rerunning the command above or by just running the command below;

readlink -f /usr/bin/java | sed "s:jre/bin/java::"

Sample output;

/usr/lib/jvm/java-17-openjdk-amd64/bin/java

Next, update the /etc/environment file to load the environment variables.

echo "JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64/bin/java" >> /etc/environment
source /etc/environment

To confirm the Java home environment variable setting, run the command below;

echo $JAVA_HOME
/usr/lib/jvm/java-17-openjdk-amd64/bin/java

Well, you are good to go. That is all on how to install Java 11|Java 17|Java 18 Ubuntu 22.04.

See other related guides by following the links below;

Install Oracle Java (OpenJDK) 13 on CentOS 8

Install Oracle Java 12 on Debian 10

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here