Upgrade Java Past apt’s OpenJDK on Raspberry Pi OS

OpenJDK 16 on Raspberry Pi OS
OpenJDK 16 on Raspberry Pi OS

The OpenJDK 11 version that comes in Raspberry Pi OS’s apt repositories is ancient. It’s so old that recently people using my Minecraft Java Server for Raspberry Pi script have started getting a warning that Minecraft 1.17 will *not* support anything below OpenJDK 16.

In this article I’m going to share a sneaky trick to update your Java to a newer version than is available in apt using snapd. Let’s begin!

Hardware Involved

Raspberry Pi 4
Raspberry Pi 4

The Raspberry Pi 4 is available in different memory configurations all the way up to 8 GB. It’s about the size of a credit card and uses an extremely low amount of power making it ideal for all sorts of projects and ideas!

Links: AliExpress*, Amazon.com*, Amazon.ca*, Amazon.com.au*, Amazon*.co.jp*, Amazon.co.uk*, Amazon.de*, Amazon.es*, Amazon.fr*, Amazon.it*, Amazon.nl*, Amazon.pl*, Amazon.se*, Amazon.sg*

Raspberry Pi 400 Kit
Raspberry Pi 400 Kit

The Raspberry Pi 400 kit includes everything you need for a full Pi 400 desktop build. The Pi 400 is the fastest Raspberry Pi ever released and comes in the form factor of a keyboard!

Links: AliExpress*, Amazon.com*, Amazon.ca*, Amazon.com.au*, Amazon.co.jp*, Amazon.co.uk*, Amazon.de*, Amazon.es*, Amazon.fr*, Amazon.it*, Amazon.nl*, Amazon.pl*, Amazon.se*, Amazon.sg*

Overview

Raspberry Pi OS’s repositories have got quite dated for Java. This is happening for other applications as well because we are waiting for the Raspberry Pi OS team to jump to the next version of Debian (the underlying OS that Raspberry Pi OS is built on) which will bring a lot of these offerings more up to date.

To get around this we’re going to use the “snapd” utility to install a much newer version of OpenJDK. We will then use update-alternatives to point our /usr/bin/java to our new JDK installation.

A reboot is required after installing snapd for the first time and it takes a lot longer to install via snap than it would via apt but since the packages are not available this is a great workaround!

OpenJDK Update Instructions

First let’s install snapd and get our reboot out of the way:

sudo apt update
sudo apt install snapd -y
sudo reboot

Once your system comes back online let’s install snap core and OpenJDK:

sudo snap install core
sudo snap install openjdk

These steps can take a little while. It’s not unusual to have to wait 5-10 minutes for each step.

Now at this point our new OpenJDK installation is installed and we just need to tell our system to use that for Java using update-alternatives:

sudo update-alternatives --install /usr/bin/java java /snap/openjdk/current/jdk/bin/java 1
sudo update-alternatives --set java /snap/openjdk/current/jdk/bin/java

And that’s it! Go ahead and verify you are using the new Java version like this:

pi@pi:~ $ java -version
openjdk version "16.0.1" 2021-04-20
OpenJDK Runtime Environment (build 16.0.1+9-snap)
OpenJDK Server VM (build 16.0.1+9-snap, mixed mode)

Bonus Tip

If you need an even newer version of Java than you are getting from the default snap there are some flags to install even newer ones. There are 3 additional release channels. These are in ascending order so the further down the list you go the newer version of OpenJDK it will retrieve:

sudo snap install openjdk --candidate
sudo snap install openjdk --beta
sudo snap install openjdk --edge

You may have to slightly modify the update-alternatives commands when using one of these alternative builds. We used /snap/openjdk/current/ in our update-alternatives commands. Check if a different folder like “edge” is being used (just ls /snap/openjdk and see what’s in there)!

Other Resources

If you’re trying to set up SSD / USB storage booting check out my Raspberry Pi USB booting setup guide

To find out where to get the 64 bit version of Raspberry Pi OS view Where to get the 64 bit Raspberry Pi OS image

For benchmarks and recommendations on the fastest storage drives/adapters for the Raspberry Pi check out my 2021 Storage Roundup

If you’re having firmware issues and need to update/restore your firmware: Raspberry Pi firmware guide here

For my new overclocking and SSD setup guide for the Pi 400 check out my Pi 400 Overclocking and SSD setup guide

Subscribe
Notify of
guest

18 Comments
Inline Feedbacks
View all comments
Sourbeans
Sourbeans
2 years ago

Hi, I’m having troubles with both of these solutions, “java -versions” now reports java 17.0.5 (latest temurin build) but when I try to run my startup script the server won’t boot up. How can I make it work?

Sourbeans
Sourbeans
2 years ago

Hi, I already have a minecraft server configured and running on RPi 3b+ and I don’t want to mess up the files or overwrite anything.
I just want to update from the default repo java 11 to 17.
Following the steps you described in your comment to bane2000 just bellow, I installed the latest adoptium build but my server now doesnt boot up using the new java binary. I don’t know what to do

Sourbeans
Sourbeans
2 years ago

Hey, thanks, I didn’t realise I was running an outdated version.
I’m on Buster though, it’s not that old I think, “sudo apt install openjdk-18” gives me “Unable to locate package openjdk-18” that’s why I came here

Sourbeans
Sourbeans
2 years ago

Well you were right, I tried to do the dirty upgrade to not have to reinstall minecraft and Pihole, but now I can’t seem to set the static IP I want, something to do with dhcp and enxb827eb2f7b1c.
I like the idea of your backup script though, I’ll look furthur into that when I’ll have everything working again.

bane2000
bane2000
2 years ago

Is there a way to down grade or add an older version as my installation of minecraft only supports 17 and I am currently on 18?

Bane2000
Bane2000
2 years ago

Thanks! Do I just copy the above for it solve my issue? Sorry for the questions, I’m a total noob with these things

CyberHare
CyberHare
3 years ago

On 64 bit ubuntu you could simply type

sudo apt install openjdk-16-jre-headless

The Bit
The Bit
3 years ago

Thank you! Worked the first time without any issues!

Nesium
Nesium
3 years ago

Thanks!