How to Install Java on Raspberry Pi

July 18, 2024

Introduction

Java is a popular programming language for creating software applications, including many web applications. Although Java comes preinstalled on most Raspberry Pi systems, some may have it missing or require reinstalling.

This guide shows how to install Java, set the default version, and specify the JAVA_HOME path on a Raspberry Pi.

How to Install Java on Raspberry Pi

Prerequisites

How to Install Java on Raspberry Pi

By default, Raspberry Pi uses the Raspbian operating system. Raspbian is based on Debian, so the terminal commands to install Java use the apt package manager.

Important: This guide will help you install the open-source OpenJDK version under the GNU license.
Oracle Java versions require an account to download, and most are not compatible with Raspberry Pi.

Step 1: Update Repositories

The default Raspbian repositories have a fully updated and compatible version of OpenJDK for the specific operating system version. Before installing it, update the repositories with:

sudo apt update

If prompted, press y to update all listed packages and allow the process to complete.

Step 2. Install OpenJDK

Install the default OpenJDK with:

sudo apt install default-jdk
sudo apt install default-jdk rpi

Press Y to continue the installation. The package installs the default newest stable version that is compatible with the operating system. To install a specific version, use:

sudo apt install openjdk-[version]-jdk

The specified version must be in the APT repositories and compatible with the operating system version.

Step 3: (Optional) Set Default Java Version

If multiple Java versions are installed, the system defaults to the latest one. To check the current Java version and see all available Java versions, run:

sudo update-alternatives --config java
sudo update-alternatives --config java output rpi

The output lists all installed Java instances on the Raspberry Pi system. The default version has an asterisk (*) next to it. Press Enter to keep the current version, or provide the selection number to switch to a different one.

To confirm the switch was successful, check the current Java version:

java –version
java -version terminal output rpi

The new version shows as the default.

Step 4: (Optional) Set JAVA_HOME Environment Variable 

Once you have set your Raspberry Pi's default Java version, you can set the <strong>JAVA_HOME</strong> path environment variable.

Note: Applications compiled in Java (e.g., Apache Tomcat) use the JAVA_HOME path environment variable to access your Java’s installation path. If not configured, you may receive an error.

To set the variable, do the following:

1. Find the location of your default Java instance:

sudo update-alternatives --config java
sudo update-alternatives --config java output path rpi

The command shows the default for each version. Press Enter to exit the prompt without making changes, and copy the path for the currently selected version.

2. Edit the /etc/environment file:

sudo nano /etc/environment

The command opens the file in nano text editor.

3. Add the following line to the file:

JAVA_HOME="[path]"
gnu nano JAVA_HOME path variable /etc/environment

Paste the path from the first step in the appropriate place.

4. Save the file and exit the text editor.

5. Reboot your system for the changes to take effect:

sudo reboot

6. After restart, use the echo command to verify the changes:

echo $JAVA_HOME
echo $JAVA_HOME rpi path output

The command shows the JAVA_HOME path defined in the /etc/environment file.

Note: The /etc/environment file is a system-wide configuration file. Anything defined in the file will affect every user on the system. To configure the path environment variable for a single user, insert the line in the .bashrc file.

Uninstall Java on Raspberry Pi

Use the apt command to uninstall Java on Raspberry Pi. The command differs depending on which version you want to remove:

  • To uninstall the default JDK, use:
sudo apt remove default-jdk
  • To uninstall a specific version, run:
sudo apt remove openjdk-[version]-jdk

Press Y, then Enter to confirm, and wait for the process to complete.

Conclusion

This tutorial showed how to install Java on Raspberry Pi. The article also showed some post-setup operations, such as setting the default Java version and defining the JAVA_HOME path environment variable.

Next, see how to set up a static IP address on Raspberry Pi.

Was this article helpful?
YesNo
Milica Dancuk
Milica Dancuk is a technical writer at phoenixNAP with a passion for programming. With a background in Electrical Engineering and Computing, coupled with her teaching experience, she excels at simplifying complex technical concepts in her writing.
Next you should read
How to Update Raspberry Pi
March 17, 2020

Update your Raspberry Pi to keep it secure. In this guide you can also...
Read more
How to Enable SSH on Raspberry Pi
February 12, 2020

This article shows you how to enable SSH on your Raspberry Pi using different...
Read more
How to Install Docker on Raspberry Pi
December 12, 2019

If you want to install Docker on Raspberry Pi, that is on its Raspian system...
Read more
How to Install Java on Ubuntu
July 18, 2024

Java is one of the most popular programming languages used for developing...
Read more