Installing Java 16 on a Raspberry Pi Zero 2 W

Installing Java 16 on a Raspberry Pi Zero 2 W is a bit of a pain and this tutorial will guide you through it.

Installing Java 16 on a Raspberry Pi Zero 2 W

Installing Java

💡
Hi, late 2023 Niko here. This piece was written in 2022 and since then OpenJDK 17 has become available via apt-get. Check out apt-cache search openjdk to see the list. (Thank you to a reader for pointing out this update!)

As of writing this (2022), Java 16 isn't readily available via apt-get on a Pi Zero 2 W. We can check this by running apt-cache search jdk

Openjdk search results.

I'm not particularly good with any Unix based OS and seeing openjdk-17-doc and openjdk-17-source in the above screenshot didn't fill me with "Oh that's the JRE, I'll just install that". Just for fun I decided to try to install the source:

Unable to install Java 17.

It didn't work. As expected.

Searching for a later Java version lead me to the split road that is OpenJDK and Oracle. I remembered that Oracle bought Java ages ago and there was a huge licencing issue in the community afterward. After reading about it for a bit, getting confused around licencing and JRE binaries for different *nix distributions I found AdoptOpenJDK (soon to be Adoptium).

The latest offered by AdoptOpenJDK is Java 16. There's an installation guide for many operating systems and I picked one for 64bit Linux as it also had instructions specifically for the Raspberry Pi OS at the bottom.

Installation Commands

For this exercise we'll be setting up apt with the AdoptOpenJDK package repository such that we can just use a sudo apt-get install call for our Java package. We could download and install the binaries ourselves, but I like the extra that apt gives us such as a common place for the repository binaries and being able to install/uninstall easily.

The following instructions are more annotated versions of the install guide.

  1. Get the needed packages.
    sudo apt-get install -y wget apt-transport-https gnupg
    This installs the following commands:
    • -y: Automatic yes to prompts
    • wget: Package for downloading files
    • apt-transport-https: Package that allows us to get from HTTPS endpoints
    • gnupg: Package for an implementation of OpenPGP for cryptography
  2. Download the AdoptOpenJDK GPG key.
    wget https://adoptopenjdk.jfrog.io/adoptopenjdk/api/gpg/key/public
    This downloads the file artifactory.gpg.public to our current directory. Here lies a regular PGP key that starts with the familiar
    -----BEGIN PGP PUBLIC KEY BLOCK-----
    We do this because we want to trust AdoptOpenJDK when using apt.
  3. Make the key accessible by apt
    gpg --no-default-keyring --keyring ./adoptopenjdk-keyring.gpg --import public
    gpg --no-default-keyring --keyring ./adoptopenjdk-keyring.gpg --export --output adoptopenjdk-archive-keyring.gpg
    After reading the documentation, it perhaps creates a keyring from the gpg file. This step I'm a little fuzzy.
  4. Clean up the unneeded file.
    rm adoptopenjdk-keyring.gpg
  5. Save the keyring in the root directory.
    sudo mv adoptopenjdk-archive-keyring.gpg /usr/share/keyrings && sudo chown root:root /usr/share/keyrings/adoptopenjdk-archive-keyring.gpg
  6. Configure AdoptOpenJDK's apt repository.
    echo "deb [signed-by=/usr/share/keyrings/adoptopenjdk-archive-keyring.gpg] https://adoptopenjdk.jfrog.io/adoptopenjdk/deb bullseye main" | sudo tee /etc/apt/sources.list.d/adoptopenjdk.list
    Our key points in this long command:
    • We're essentially putting the whole string in the echo call into a file that holds our package repositories: /etc/apt/sources.list.d/adoptopenjdk.list.
    • The tee command pipes the data to both the file we're writing to and the console for us to see.
    • The argument bullseye comes from getting the codename of the OS. Raspberry Pi OS is from Debian (which is also why the command starts with deb) whose codename is bullseye. We can look this up ourselves by running: cat /etc/os-release | grep VERSION_CODENAME | cut -d = -f 2
  7. Refresh our package indexes.
    sudo apt-get update
  8. List the AdtopOpenJDK packages.
    sudo apt-cache search adoptopenjdk
  9. Select the one to install. I opted for the Java 16 option.
    sudo apt-get install adoptopenjdk-16-hotspot-jre

Or, if you want all 2:53 of me running through these:

0:00
/2:53

With that, we can now run java --version and we should see that Java 16 is installed (you may need to open another terminal before trying this).

Java 16 installed successfully.

Woohoo! That's it. Enjoy all the fruits Java gives you on a tiny Zero 2 W!