In this guide, we will show you how to install Java for the Raspberry Pi.
Java is a popular and powerful programming language that has a wide range of uses. The Java language is the language that the popular game Minecraft is built with.
In this tutorial, we will be installing the OpenJDK version of Java and not the one provided by Oracle.
The reason for this is that the OpenJDK version of Java is more up to date for ARM-based systems such as the Raspberry Pi. It is also easily obtainable through the Raspberry Pi OS repository.
There are two possible flavors of Java that you can install. There is the JDK, which stands for “Java Development Kit” and the JRE, which stands for “Java Runtime Environment”.
In this guide, we will be showing you how to install the JDK to your Raspberry Pi. The JDK comes with the JRE but will also allow you to compile your own Java programs as well.
If you only install the JRE, then you will only be able to run pre-compiled programs.
We use Java in our Raspberry Pi Minecraft Server tutorial as it is required for the software to work.
Equipment List
Here is all the equipment that we recommend for this tutorial on installing Java.
Recommended
- Raspberry Pi ( Amazon ) 3 or Newer
- Micro SD Card ( Amazon )
- Power Supply ( Amazon )
- Ethernet Cable ( Amazon ) or Wi-Fi ( Amazon )
Optional
We last tested this tutorial on a Raspberry Pi 5 running the latest version of Raspberry Pi OS Bookworm.
Installing the Latest Version of Java to the Raspberry Pi
In this section, we will show you how to install the latest version of Java on your Raspberry Pi.
Unfortunately, the Raspberry Pi’s package repository often contains an out-of-date version of Java. While this isn’t an issue for some software, some rely on newer versions, such as the Minecraft server.
Luckily, we can get newer builds of Java for our Raspberry Pi by adding a third-party repository. This repository is managed by Azul and offers builds of newer versions of Java for ARM64 systems like our Raspberry Pi.
The caveat is that these steps must work on a Raspberry Pi 3 or later and a 64-bit operating system.
Preparing your Raspberry Pi for Java
1. Before we can add the Azul repository and download the latest version of Java to our Raspberry Pi, we must ensure we have an updated base to work off of.
You can update the package list cache and upgrade any out-of-date packages using the following two commands.
sudo apt update
sudo apt upgrade
2. Your next step is to install the packages we require to set up the Azul repository.
sudo apt install curl gnupg ca-certificates
Adding the Azul Repository to your Pi
3. Using the following command, we will download and save the Azul GPG key to our Raspberry Pi.
We need this key to verify the packages we are downloading are authentic.
curl -s https://repos.azul.com/azul-repo.key | sudo gpg --dearmor -o /usr/share/keyrings/azul.gpg
4. With the GPG key now saved to our system, our next step is to add the Azul repository to our list of sources.
Adding this repository will give us access to the latest versions of Java on our Raspberry Pi,
echo "deb [arch=arm64 signed-by=/usr/share/keyrings/azul.gpg] https://repos.azul.com/zulu/deb stable main" | sudo tee /etc/apt/sources.list.d/zulu.list
5. For our system to realize that it can install the new packages, you will want to use the following command to update the package list cache.
sudo apt update
Installing the Latest LTS Version of Java on your Raspberry Pi
6. With the Azul repository now added to our Raspberry Pi, we can install OpenJDK 21 by using the following command.
At the time of publishing, OpenJDK 21 was the current long-term support release. You can install newer versions if you like, as Azul typically has builds for the latest release.
sudo apt install zulu21-jdk-headless
7. You can verify the version of Java you just installed by running the following command within the terminal.
java --version
Below you can see that we now have OpenJDK 21 (Java 21) running on our Raspberry Pi using the builds from Azul.
openjdk 21.0.3 2024-04-16 LTS
OpenJDK Runtime Environment Zulu21.34+19-CA (build 21.0.3+9-LTS)
OpenJDK 64-Bit Server VM Zulu21.34+19-CA (build 21.0.3+9-LTS, mixed mode, sharing)
Installing Java to the Raspberry Pi from the Debian Repository
This section will show you how to install Java on your Raspberry Pi using the standard repository.
It’s important to note that this method comes with a limitation. You will be using the version of Java that was initially shipped with your distribution, such as OpenJDK 17 with Raspberry Pi OS Bookworm.
1. Before we go ahead and install Java, we need first to ensure that everything is up to date.
To update all existing packages, go ahead, and run the following two commands.
sudo apt update
sudo apt upgrade
The update process can take some time if you have a slow network connection, or there is a lot of packages to update.
2. Once the update process has completed, we can proceed to install the latest available version of Java to our Raspberry Pi by running the following command.
We are using the package default-jdk
as this will always point to the latest available version of the JDK for Raspberry Pi OS.
For example, on Bookworm, this command will install Java 17 to our Pi.
sudo apt install default-jdk
3. Now that we have installed Java, let us quickly run it to make sure everything is working.
To test that it has been installed correctly, run the following command on the Raspberry Pi.
java -version
All this command does is get the Java Runtime to print out its version.
openjdk version "11.0.5" 2019-10-15
OpenJDK Runtime Environment (build 11.0.5+10-post-Raspbian-1deb10u1)
OpenJDK Server VM (build 11.0.5+10-post-Raspbian-1deb10u1, mixed mode)
Conclusion
Hopefully, at this point, you will now have Java up and running on your Raspberry Pi.
We have shown you two different methods to install this software on your Pi. The first is to use the Azul repository sops, which allows you to run the latest versions of Java. The second is to use the builds provided through the Debian repository, which are typically older releases.
Please feel free to drop a comment below if you have run into any issues or have any feedback on this tutorial.
If you liked this tutorial, feel free to check out our many other Raspberry Pi projects.
Unfortunately the Zulu method didn’t work for me on a Raspberry 2 running raspberry OS lite 32bit. Because there are no packages for arnhf.
So I found a solution with the Bellsoft Liberica Standard JDK 21.0.4+9 arm 32 for Linux.
Download:
Install:
Maybe it works for someone else who runs into that problem.
Still striking the exact same error message as Tom and Roger showed, “unable to locate package zulu21-jdk-headless”. As with previous issues, architecture is aarch64, however specifying arm64 doesn’t seem to have fixed it.
sudo apt update gives:
“Hit:1 https://repos.azul.com/zulu/deb stable InRelease
Hit:2 http://archive.raspberrypi.org/debian bullseye InRelease
Hit:3 http://raspbian.raspberrypi.org/raspbian bullseye InRelease
Reading package lists… Done
Building dependency tree… Done
Reading state information… Done
All packages are up to date.”
Hi Octoscorp,
Could you please try running the following command and respond with what it returns?
Kind regards,
Emmet
I have the same problem Roger had.
tom@raspberrypi:~ $ sudo apt install zulu21-jdk-headless
Reading package lists… Done
Building dependency tree… Done
Reading state information… Done
E: Unable to locate package zulu21-jdk-headless
So I checked my system – and it’s 64 bit,
tom@raspberrypi:~ $ lscpu
Architecture: aarch64
Up to that point at the very end of the process these instructions were excellent. I think the effort you made with layout enhances the clarity.
Tom
Hi Tom,
I am still unable to replicate the issue on my end. I have, however, made a slight adjustment to hopefully rectify the issues. Basically, I changed step 4 so that we specifically specify the “arm64” architecture when adding the repository.
If this doesn’t solve the issue, can you run “sudo apt update” you should see a list of the repositories that your system is pulling from and whether it could successfully pull the package list.
Kind regards,
Emmet
sudo apt install zulu21-jdk-headless
Get this:
Unable to locate package zulu21-jdk-headless
Hi Roger,
Ensure that you have ran “sudo apt update” after adding the Azul repository.
If you are still running into the issue its likely that you are running a 32-bit system instead of a 64-bit one.
Kind regards,
Emmet
it works fore me, thx
After discovering that the Bullseye Raspberry Pi OS did not have Java in the wrapper, I found this guide and it worked great. Thanks for a great tutorial!