How to install Java on a Raspberry Pi

There are two different Java implementations, Oracle Java and OpenJDK. This tutorial explains how to install Java (OpenJDK) on a Raspberry Pi with the latest Raspbian operating system running on it.

Java is one of the most popular programming languages ​​used to build various types of applications and systems. There are two different Java implementations, Oracle Java and OpenJDK. This tutorial explains how to install Java (OpenJDK) on a Raspberry Pi with the latest Raspbian operating system running on it.

Install Java 11 on Raspberry Pi

OpenJDK 11 is the default Java development in the latest Raspbian operating system, based on Debian 10, Buster.

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

sudo apt update sudo apt install default-jdk

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

java -version

The output will look 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)

You have successfully installed Java on the Pi and you can start using it.

How to install Java on a Raspberry Pi Picture 1How to install Java on a Raspberry Pi Picture 1 Installing Java on the Raspberry Pi is simple!

Install Java 8 on Raspberry Pi

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

sudo apt update sudo apt install openjdk-8-jdk

Verify installation by printing Java version:

java -version

The output will look 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 Pi, run java -version command to check default version:

java -version

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

sudo update-alternatives --config java

The output will look 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 to keep the current choice[*], or type selection number: 

You should see a list of all installed Java versions. Enter the version number you want to set as default and press Enter.

Environment variable JAVA_HOME

The JAVA_HOME environment variable is used by some Java applications to determine where to install Java. To set the JAVA_HOME environment variable, use the update-alternatives command to find out where to install Java:

sudo update-alternatives --config java

In this example, the installation paths are as follows:

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

After you find 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/bin/java"

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

source /etc/environment

To verify that the JAVA_HOME variable has been set, type:

echo $JAVA_HOME

You will see the path to the Java 11 binary:

/usr/lib/jvm/java-11-openjdk-armhf/bin/java

The / etc / environment is a system-wide configuration file that is used by all users. If you want to set the JAVA_HOME variable on a per-user basis, add the line to .bashrc or any other configuration file loaded when the user logs in.

4.3 ★ | 10 Vote