A step-by-step guide to installing the latest version of Java (Java 17) on your Raspberry Pi

Learn how to install the latest version of Java on your Raspberry Pi and get started with Java programming. This tutorial will show you how to set up Java 17, which is the most recent version as of th …


Updated September 15, 2023

Need help with your Raspberry Pi?
Contact Me!

Do you love silly Raspberry Pi Projects?
Check out my this YouTube Channel!


Learn how to install the latest version of Java on your Raspberry Pi and get started with Java programming. This tutorial will show you how to set up Java 17, which is the most recent version as of this writing.

Introduction

Java is a high-level programming language that can be used for developing applications for various platforms, including desktop, mobile, and web. With its popularity growing rapidly, it’s important to stay updated with the latest versions to ensure compatibility with new technologies and security patches. In this tutorial, we will guide you through installing Java 17 on your Raspberry Pi.

Prerequisites

To follow along with this tutorial, make sure you have:

  • A Raspberry Pi device (either model 2, 3, or 4)
  • An SD card with a Raspbian OS installed (Raspberry Pi OS is also fine)
  • An internet connection for your Raspberry Pi

Step 1: Update the system and install required packages

First, open a terminal window on your Raspberry Pi and update the package lists using the following command:

sudo apt-get update

Next, install the necessary packages to build Java from source code. Run the following command:

sudo apt-get install -y git wget curl build-essential autoconf libtool libx11-dev libxext-dev libxrender-dev libxtst-dev libxi-dev libxrandr-dev libxinerama-dev libxt-dev mesa-utils unzip

Step 2: Install GCC (GNU Compiler Collection)

Java requires a C compiler to build from source code. The Raspberry Pi OS comes with GCC preinstalled, but it may be outdated. To ensure you have the latest version, run these commands:

sudo apt-get install -y gcc

Check your GCC version using this command:

gcc --version

If your version is older than 10, you will need to update it. You can do so by adding the Raspberry Pi PPA and installing the latest version of GCC:

sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y
sudo apt-get update
sudo apt-get install -y gcc-10 g++-10

Set GCC 10 as your default C compiler:

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 60 --slave /usr/bin/g++ g++ /usr/bin/g++-10

Step 3: Install AdoptOpenJDK 17

AdoptOpenJDK is an open-source version of Java that includes optimizations for ARM processors. We will use this to install Java 17 on our Raspberry Pi.

First, download the latest release of AdoptOpenJDK 17 for Linux ARM:

wget https://github.com/AdoptOpenJDK/openjdk17-binaries/releases/download/jdk-17%2B35/OpenJDK17U-jdk_aarch64_linux_hotspot_17_35.tar.gz

Next, extract the downloaded file and move it to /opt:

sudo mkdir /opt/java
sudo tar -xvf OpenJDK17U-jdk_aarch64_linux_hotspot_17_35.tar.gz -C /opt/java --strip-components=1

Finally, set the JAVA_HOME and PATH environment variables:

echo "export JAVA_HOME=/opt/java" >> ~/.bashrc
echo "export PATH=$PATH:$JAVA_HOME/bin" >> ~/.bashrc
source ~/.bashrc

Step 4: Verify the installation

To verify that Java 17 is installed correctly, run the following command:

java -version

If everything went well, you should see output similar to this:

openjdk version "17" 2021-09-14
OpenJDK Runtime Environment AdoptOpenJDK (build 17+35)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 17+35, mixed mode, sharing)

Congratulations! You have successfully installed Java 17 on your Raspberry Pi. Now you can start developing and running Java applications on your device.

Conclusion

In this tutorial, we learned how to install the latest version of Java (Java 17) on a Raspberry Pi. With Java installed, you can now develop and run Java applications on your device. You can also explore other programming languages available for Raspberry Pi or use it as a server to host web applications.