Installing Java on a Raspberry Pi

Installing Java on a Raspberry Pi

Introduction

Java is one of the most widely used software platforms for developing and running software applications. Java is used by many web-based applications.

You can use this guide to install Java on a Raspberry Pi. Moreover, the article describes how to set the default Java version and specify the default JAVA_HOME path environment variable.


Prerequisites

A Raspberry Pi 2, 3, or 4

User accounts with sudo privileges

Having access to a terminal/command line

Note on Java and Raspberry Pi

You can skip ahead to the next section if you're already familiar with Java and Raspberry Pi.

Java comes in two main versions:

  • OpenJDK – An open-source Java platform licensed under the GNU General Public License.
  • Oracle Java – A paid service that includes support and licensing options.

Important-You can follow this guide to install the free, open-source OpenJDK version. Most Oracle Java versions do not work on Raspberry Pi, and require an account to download.

You may see the following abbreviations when browsing different Java versions:

  • SE: Standard Edition (usually for Oracle Java)
  • JRE: Java Runtime Environment (for running Java applications)
  • JDK: Java Development Kit (for writing and running Java applications)
  • LTS: Long-Term Support (major software versions intended for long-term use)

Installing Java on Raspberry Pi

Raspberry Pi runs Raspbian as its default operating system. Raspbian is based on Debian, so the apt package manager will be used to install Java.

The default Raspbian software repositories should be used to install all packages. Many packages are incompatible with Raspberry Pi's ARM-based system architecture.

Installing OpenJDK Java 11 on Raspberry Pi

Raspbian ships with an updated and compatible version of OpenJDK.

1. In a terminal window, type:

"sudo apt update

sudo apt install default-jdk"



Your package repositories will be updated with the latest version of software using the first command. The second command will install Java.

2. If prompted, type Y and press Enter to complete the process.

3. Verify the installation by checking the software version as follows:

"java –version"

The software version will be displayed. It should look like this:


Depending on the latest Java version, your version may differ.

Install OpenJDK Java 8 on the Raspberry Pi

1. To install OpenJDK 8, run the following commands in a terminal window:

"sudo apt update

sudo apt install default-jdk"


In the first command, you update your package repositories to use the latest software. In the second command, you install Java.

2. If prompted, type Y and press Enter to complete the process.

3. The software version can be checked by checking the installation as follows:

"java –version"

It will display the software version. A screen similar to this should appear:


Depending on the latest Java version, your version may differ.

Install OpenJDK Java 8 on the Raspberry Pi

1. To install OpenJDK 8, run the following commands in a terminal window:
"sudo apt update
sudo apt install openjdk-8-jdk"


The first command updates the list of software packages in your repository. Installing Java OpenJDK 8 is the second command.
2. If necessary, hit Enter to confirm and allow the process to continue.
3. You can verify the installation by:

"java –version"
The software version will be displayed. It should look like this:

Note:You will see the Java 11 version if you installed Java 11 before Java 8. The system will use the most recent Java version by default.
Continue to the next section to manually set the default Java version.

Set Default Java Version (Optional)

Your default version will likely be OpenJDK 11, if you have installed both versions. Check the Java version on your system by:
"java –version"
1. Begin by running the following command to manually set a different Java version:
"sudo update-alternatives --config java"
The output will list out all installed Java instances on your Raspberry Pi system.

2. A * will appear next to the default version. To set a different Java version as the default, type a selection number and hit Enter.
See the following example for more details:

Set JAVA_HOME Path Environment Variable (Optional)

Once you have set your Raspberry Pi’s default version of Java, you can set the JAVA_HOME path environment variable.

Note- JAVA_HOME path environment variable is used by Java applications (e.g., Apache Tomcat) to access the Java installation path. You may receive the error "[fail] no JDK found" if not configured.
1. Locate your default Java instance:
"sudo update-alternatives --config java"
Raspberry Pi systems have the following default locations:
OpenJDK 11 path – /usr/lib/jvm/java-11-openjdk-armhf/bin/java
OpenJDK 8 path – /usr/lib/jvm/java-8-openjdk-armhf/jre/bin/java
2. Edit the /etc/environment file as follows:
"sudo nano /etc/environment"
3. Add the following information to the file:
"JAVA_HOME="path_of_your_default_version_of_java"
You should insert the following for OpenJDK 8:
"JAVA_HOME="/usr/lib/jvm/java-8-openjdk-armhf/jre/bin/java"
4. Exit the text editor and save the file.
5. Reboot your computer to apply the changes:
"sudo reboot"
6. Run the following command to verify the changes:
"echo $JAVA_HOME"
The output should contain the JAVA_HOME path defined in the /etc/environment file.
Note: The /etc/environment file is a system-wide configuration file. Anything defined in this file affects every user on the system. Add the line to the .bashrc file to configure the path environment variable for a single user.

Uninstall Java on Raspberry Pi

Using the apt package manager, you have successfully installed Java 8 and/or Java 11. It is also possible to uninstall Java using apt.

Run the following command to uninstall Java 8 on your Raspberry Pi system:
"sudo apt remove openjdk-8-jdk"
Once the process is complete, hit Y and enter to confirm.
You can uninstall Java 11 on your Raspberry Pi system by running the following command:
"sudo apt remove default-jdk"
If you would like to confirm, hit Y and enter.

Conclusion


This tutorial demonstrated how to install Java 8 and Java 11 on a Raspberry Pi. Hopefully, you now have a working installation of OpenJDK 11 and/or OpenJDK 8 on your Raspberry Pi.

Furthermore, the article guided you through setting the JAVA_HOME path environment variable and setting the default version of Java. Refer to our guides How To Set Environment Variables In MacOS or How To Set Environment Variables In Linux for more similar tutorials.