Install Java 17 (OpenJDK 17) on Ubuntu 22.04|20.04|18.04

Posted on 116 views

Welcome to this guide on how to install Java 17 (OpenJDK 17) on Ubuntu 22.04|20.04|18.04. Java is a high-level object-oriented programming language and computing platform intended to let application developers write once and run everywhere. This means that a compiled java code runs on all platforms that support Java without the need for recompilation. JDK is a collection of various programming tools such as JRE(Java Runtime Environment), Java, Javac, Jar, and many others. Java 17 LTS is the latest long-term support release for the Java SE platform released on 14 September 2021.

Java 17 comes with the following amazing features:

  • Enhanced pseudo-random number generators
  • New rendering pipeline for MacOS, using the Apple Metal API as an alternative to the existing pipeline that uses the deprecated OpenGL API
  • Remove the Experimental AOT and JIT Compiler
  • Sealed classes and interfaces restrict which other classes or interfaces may extend or implement them.
  • Removal of the Remote Method Invocation (RMI) Activation mechanism
  • Deprecate the Applet API for Removal
  • Porting the JDK to MacOS/AArch64 in response to Apple’s plan to transition its Macintosh computers from x64 to AArch64
  • Strong encapsulation for JDK internals
  • Context-specific deserialization filters
  • The foreign function and memory API which allows Java programs to interoperate with code and data outside of the Java runtime

With the above information, we are now set to dive into the Java 17 LTS installation. In this guide, we will cover 2 methods to get Java 17 installed on Ubuntu 22.04|20.04|18.04.

  1. OpenJDK from APT repos
  2. OpenJDK Manually
  3. Oracle JDK/JRE from PPA
  4. Oracle JDK manual install

Option 1 – Install OpenJDK 17 on Ubuntu 22.04|20.04|18.04 from APT

The easiest and quickest way is installation OpenJDK 17 on Ubuntu 22.04|20.04|18.04 from OS upstream repositories.

sudo apt update
sudo apt install openjdk-17-jdk

For JRE run the following commands to install

sudo apt install openjdk-17-jre

Check Java version to validate a successful installation:

$ 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)

Option 2 – Install OpenJDK 17 on Ubuntu 22.04|20.04|18.04 Manually

Java OpenJDK 17 is an open-source implementation of the Java SE platform. Since the OpenJDK versions available in the default repositories are not up to date, we will have to download the Open-source JDK 17 using the wget command.

### Linux 64-bit ###
wget https://download.java.net/java/GA/jdk17.0.2/dfd4a8d0985749f896bed50d7138ee7f/8/GPL/openjdk-17.0.2_linux-x64_bin.tar.gz

### Linux ARM64 ###
wget https://download.java.net/java/GA/jdk17.0.2/dfd4a8d0985749f896bed50d7138ee7f/8/GPL/openjdk-17.0.2_linux-aarch64_bin.tar.gz

Extract the downloaded tarball.

### Linux 64-bit ###
tar xvf openjdk-17.0.2_linux-x64_bin.tar.gz

### Linux ARM64 ###
tar xvf openjdk-17.0.2_linux-aarch64_bin.tar.gz

Then move the extracted file to the /opt/ directory as shown.

sudo mv jdk-17.0.2/ /opt/jdk-17/

Set the environment variables.

echo 'export JAVA_HOME=/opt/jdk-17' | sudo tee /etc/profile.d/java17.sh
echo 'export PATH=$JAVA_HOME/bin:$PATH'|sudo tee -a /etc/profile.d/java17.sh
source /etc/profile.d/java17.sh

Verify the Java installation as below.

$ echo $JAVA_HOME
/opt/jdk-17

Check the installed version of Java.

$ java --version
openjdk 17.0.2 2022-01-18
OpenJDK Runtime Environment (build 17.0.2+8-86)
OpenJDK 64-Bit Server VM (build 17.0.2+8-86, mixed mode, sharing)

Option 3 – Install Oracle JDK 17 on Ubuntu 22.04|20.04|18.04

For this option, we download the production-ready Java from the Java SE Downloads page.

Alternatively, one can install Oracle JDK 17 on Ubuntu using the “Linux Uprising” team PPA installer script which automatically downloads and sets the default Java version on Ubuntu 64-bit based systems.

Clean previous OpenJDK installation:

sudo rm /etc/profile.d/java17.sh
exit

Add the PPA repository to your Ubuntu system.

sudo add-apt-repository ppa:linuxuprising/java

Then install Oracle JDK 17 as shown.

sudo apt update
sudo apt install oracle-java17-installer

Accept installation prompts:

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  binutils binutils-common binutils-x86-64-linux-gnu gsfonts gsfonts-x11 java-common libbinutils libctf-nobfd0 libctf0 libfontenc1 oracle-java17-set-default x11-common xfonts-encodings
  xfonts-utils
Suggested packages:
  binutils-doc binfmt-support visualvm ttf-baekmuk | ttf-unfonts | ttf-unfonts-core ttf-kochi-gothic | ttf-sazanami-gothic ttf-kochi-mincho | ttf-sazanami-mincho ttf-arphic-uming firefox
  | firefox-2 | iceweasel | mozilla-firefox | iceape-browser | mozilla-browser | epiphany-gecko | epiphany-webkit | epiphany-browser | galeon | midbrowser | moblin-web-browser | xulrunner
  | xulrunner-1.9 | konqueror | chromium-browser | midori | google-chrome
The following NEW packages will be installed:
  binutils binutils-common binutils-x86-64-linux-gnu gsfonts gsfonts-x11 java-common libbinutils libctf-nobfd0 libctf0 libfontenc1 oracle-java17-installer oracle-java17-set-default x11-common
  xfonts-encodings xfonts-utils
0 upgraded, 15 newly installed, 0 to remove and 68 not upgraded.
Need to get 6260 kB of archives.
After this operation, 20.0 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

You will see this installer window where you are supposed to agree to the License Terms.

Install-Java-17-OpenJDK-17-on-Ubuntu-20.0418.04-1

Agree to the License Terms by clicking Yes

Install-Java-17-OpenJDK-17-on-Ubuntu-20.0418.04-1

Verify the installed version of Java by checking the version.

$ java -version
java version "17.0.1" 2021-10-19 LTS
Java(TM) SE Runtime Environment (build 17.0.1+12-LTS-39)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.1+12-LTS-39, mixed mode, sharing)

$ javac -version
javac 17

Option 4 – Manually install Oracle JDK 17 on Ubuntu 22.04|20.04|18.04

The official Oracle JDK is a development environment for building applications and components using the Java programming language. This toolkit includes tools for developing and testing programs written in the Java programming language and running on the Java platform.

We’ll download Oracle JDK 17 Debian installer

wget https://download.oracle.com/java/17/archive/jdk-17.0.3_linux-x64_bin.deb

Run the installer after it’s downloaded:

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

Additional dependencies should be installed automatically:

Reading package lists... Done
Building dependency tree
Reading state information... Done
Note, selecting 'jdk-17' instead of './jdk-17.0.3_linux-x64_bin.deb'
The following additional packages will be installed:
  libc6-i386 libc6-x32 libxtst6
The following NEW packages will be installed:
  jdk-17 libc6-i386 libc6-x32 libxtst6
0 upgraded, 4 newly installed, 0 to remove and 63 not upgraded.
Need to get 5517 kB/161 MB of archives.
After this operation, 346 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

Set JAVA environment

echo 'export JAVA_HOME=/usr/lib/jvm/jdk-17/' | sudo tee -a /etc/profile
echo 'export PATH=$PATH:$JAVA_HOME/bin' | sudo tee -a /etc/profile

Source the file and confirm Java version:

$ source /etc/profile

$ echo $JAVA_HOME
/usr/lib/jvm/jdk-17/

$ java -version
java version "17.0.2" 2022-01-18 LTS
Java(TM) SE Runtime Environment (build 17.0.2+8-LTS-86)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.2+8-LTS-86, mixed mode, sharing)

Set Default Java Version on Ubuntu 22.04|20.04|18.04

Setting the default Java version applies in instances where you have multiple Java versions installed on your system.

First, you need to list all the available versions.

sudo update-alternatives --config java

Output:

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/java-17-oracle/bin/java          1091      manual mode

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

From the output, select the Java version you want to set as your default version by entering the number as shown above.

Verify the set version

$ java -version
java version "17.0.1" 2021-10-19 LTS
Java(TM) SE Runtime Environment (build 17.0.1+12-LTS-39)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.1+12-LTS-39, mixed mode, sharing)

Setting JAVA_HOME Environment Variable.

Setting the JAVA_HOME environment variable is important as it is used by Java applications to determine the install location of Java and the exact version to use when running the applications.

We will set a persistent path so we edit the file /etc/profile as below.

sudo vi /etc/profile

In the file, add the Java path as shown.

JAVA_HOME="/path/to/java/install"

In this case, my Java path will be.

JAVA_HOME="/usr/lib/jvm/java-17-oracle"

For these changes to apply, you require to log in and log out or use the source command.

source /etc/environment

Verify the set variables.

$ echo $JAVA_HOME
/usr/lib/jvm/java-17-oracle

Test the Java Installation

We will now test the Java installation using a simple HTML file. In this guide, we will create an HTML file with the name Hello_World.java.

cat > Hello_World.java <Hello Java World from Kenya! Java 17 is amazing!");
  

EOF

Compile the code:

java Hello_World.java

Sample Output:

$ java Hello_World.java
Hello Java World from Kenya! Java 17 is amazing!

Conclusion

Congratulations! You have triumphantly installed Java 17 (OpenJDK 17) on Ubuntu 22.04|20.04|18.04. In addition to that, you have learned how to configure Java alternatives and set the JAVA_HOME Environment Variable. I hope this was helpful.

coffee

Gravatar Image
A systems engineer with excellent skills in systems administration, cloud computing, systems deployment, virtualization, containers, and a certified ethical hacker.