How to Install Java on Raspberry Pi

Updated on

4 min read

Install Java on Raspberry Pi

Java is one of the most popular programming languages used to build different kinds of applications and systems.

There are two different implementations of Java, Oracle Java and OpenJDK. OpenJDK is an open-source implementation of the Java Platform. Oracle Java has a few additional commercial features and a license that permits only non-commercial use, such as personal or development use.

This guide explains how to install Java (OpenJDK) on Raspberry Pi with the latest Raspbian OS running on it.

The standard Raspbian repositories include two different Java packages, Java Runtime Environment (JRE) and Java Development Kit (JDK). JRE includes the Java virtual machine (JVM), classes, and binaries that allow you to run Java programs. JDK consist of JRE and development/debugging tools and libraries necessary to build Java applications.

If you are not sure which Java package to install, the general recommendation is to stick to the default OpenJDK (JDK 11) version. Some Java-based applications may require a specific version of Java, so you should consult the application documentation.

Installing Java 11 on Raspberry Pi

OpenJDK 11 is the default Java development and runtime in the latest Raspbian OS, which is based on Debian 10, Buster.

Run the following commands to install the OpenJDK 11 JDK on your Raspberry Pi:

sudo apt updatesudo apt install default-jdk

Once the installation is complete, verify it by checking the Java version:

java -version

The output should look something like this:

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)

That’s it! You have successfully installed Java on your Pi, and you can start using it.

Installing Java 8 on Raspberry Pi

The previous Java LTS version 8 is still supported and widely used. If your application requires Java 8, install it by typing:

sudo apt updatesudo apt install openjdk-8-jdk

Verify the installation by printing the Java version :

java -version

The output should look something like this:

openjdk version "1.8.0_212"
OpenJDK Runtime Environment (build 1.8.0_212-8u212-b01-1+rpi1-b01)
OpenJDK Client VM (build 25.212-b01, mixed mode)

Set the Default Version

If you have multiple Java versions installed on your Pi, run the java -version command to check the default version:

java -version

To change the default version, use the update-alternatives tool:

sudo update-alternatives --config java

The output will look something like below:

There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-11-openjdk-armhf/bin/java      1111      auto mode
  1            /usr/lib/jvm/java-11-openjdk-armhf/bin/java      1111      manual mode
  2            /usr/lib/jvm/java-8-openjdk-armhf/jre/bin/java   1081      manual mode

Press <enter> to keep the current choice[*], or type selection number: 

You will be presented with a list of all installed Java versions. Enter the number of the version you want set as the default and press Enter.

JAVA_HOME Environment Variable

The JAVA_HOME environment variable is used by some Java applications to determine the Java installation location.

To set the JAVA_HOME environment variable, use the update-alternatives command to find where Java is installed:

sudo update-alternatives --config java

In this example, the installation paths are as follows:

  • OpenJDK 11 is located at /usr/lib/jvm/java-11-openjdk-armhf/bin/java
  • OpenJDK 8 is located at /usr/lib/jvm/java-8-openjdk-armhf/jre/bin/java

Once you found the path of the Java installation, open the /etc/environment file:

sudo nano /etc/environment

Assuming you want to set JAVA_HOME to OpenJDK 11, add the following line, at the end of the file:

/etc/environment
JAVA_HOME="/usr/lib/jvm/java-11-openjdk-armhf"

For changes to take effect on your current shell you can either log out and log in or run the following source command:

source /etc/environment

To verify that the JAVA_HOME variable is set, type:

echo $JAVA_HOME

You should see the path to the Java 11 binary:

/usr/lib/jvm/java-11-openjdk-armhf
/etc/environment is a system-wide configuration file, which is used by all users. If you want to set the JAVA_HOME variable on a per-user basis, add the line to the .bashrc or any other configuration file which is loaded when the user logs in.

Uninstall Java

You can uninstall Java like any other package installed with apt .

For example, to uninstall the default-jdk package simply run:

sudo apt remove default-jdk

Conclusion

The latest LTS version of OpenJDK is available in the default Raspbian repositories, and the installation is a simple and straightforward task.

If you have any questions, feel free to leave a comment.