How to Install Java on Raspberry Pi

Ben
Ben
@benjislab

Java is a versatile and powerful programming language that allows you to develop and run a wide range of applications. Installing Java on your Raspberry Pi enables you to leverage this powerful language for various projects, from simple applications to complex development environments. This guide will walk you through the steps to install Java on your Raspberry Pi.

Prerequisites

Before starting, ensure you have the following:

  • Raspberry Pi (any model)
  • MicroSD card with Raspberry Pi OS installed
  • Stable internet connection
  • Access to the command line (via monitor and keyboard or SSH)

Step 1: Update and Upgrade Your Raspberry Pi

First, make sure your Raspberry Pi is up to date.

  1. Open a terminal on your Raspberry Pi or SSH into it.
  2. Run the following commands to update and upgrade your system:
sudo apt update
sudo apt upgrade -y

Step 2: Install Java Development Kit (JDK)

The Java Development Kit (JDK) includes the Java Runtime Environment (JRE) and development tools necessary for developing and running Java applications.

  1. Install OpenJDK:

OpenJDK is an open-source implementation of the Java Platform. To install OpenJDK on your Raspberry Pi, run the following command:

sudo apt install default-jdk -y
  1. Verify the Installation:

After the installation is complete, verify that Java is installed correctly by checking the version:

java -version

You should see output indicating the version of Java that is installed, such as:

openjdk version "11.0.11" 2021-04-20
OpenJDK Runtime Environment (build 11.0.11+9-post-Raspbian-1deb10u1)
OpenJDK Server VM (build 11.0.11+9-post-Raspbian-1deb10u1, mixed mode)

Step 3: Set Up JAVA_HOME Environment Variable

Setting up the JAVA_HOME environment variable ensures that other applications and development tools can find your Java installation.

  1. Find the Java Installation Path:

Run the following command to find the installation path of Java:

readlink -f /usr/bin/java | sed "s:bin/java::"

This command will output the path to your Java installation, such as /usr/lib/jvm/java-11-openjdk-armhf/.

  1. Set the JAVA_HOME Variable:

Open the .bashrc file in your home directory:

nano ~/.bashrc

Add the following line to the end of the file, replacing the path with the one you obtained:

export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-armhf/
export PATH=$PATH:$JAVA_HOME/bin
  1. Apply the Changes:

Save the file and apply the changes by running:

source ~/.bashrc

Step 4: Test Your Java Installation

To ensure that Java is installed and configured correctly, you can compile and run a simple Java program.

  1. Create a Test Java Program:

Create a new file named Test.java:

nano Test.java

Add the following code to the file:

public class Test {
    public static void main(String[] args) {
         System.out.println("Java is working on Raspberry Pi!");
    }
}
  1. Compile the Java Program:

Compile the Java program using the javac command:

javac Test.java

This will create a Test.class file in the same directory.

  1. Run the Java Program:

Run the compiled Java program using the java command:

java Test

You should see the following output:

Java is working on Raspberry Pi!

Conclusion

By installing Java on your Raspberry Pi, you can expand its capabilities to run and develop Java applications. Whether you're using it for educational purposes, development projects, or running specific Java-based applications, having Java installed on your Raspberry Pi provides a versatile toolset. Follow these steps to get Java up and running on your Raspberry Pi and start exploring the possibilities of Java programming.