How to install Java on a Raspberry Pi

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 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

May be interested

  • How to install multiple Java versions on CentOS 8How to install multiple Java versions on CentOS 8
    java is one of the most widely used high-level programming languages. in this article, readers will learn how to install different versions of java on centos 8.
  • How to install an operating system for Raspberry PiHow to install an operating system for Raspberry Pi
    the article will show you how to install and run your new operating system on pi, as well as how to copy your perfect settings to fix errors quickly.
  • What is Java? Why choose Java?What is Java?  Why choose Java?
    what is java? java is a high-level programming language, developed by sun microsystem and released in 1995. java runs on a variety of platforms, such as windows, mac and various versions of unix.
  • How to install NordVPN on Raspberry PiHow to install NordVPN on Raspberry Pi
    you've always wanted to get the most out of your raspberry pi and in today's article, we will share tips on how to easily configure openvpn encryption on your device.
  • How to install LibreELEC on Raspberry Pi 4How to install LibreELEC on Raspberry Pi 4
    the raspberry pi 4 is the most powerful piece of hardware to come from the pi platform. it has usb 3.0, more ram and faster cpu. for this reason, the pi 4 is perfect for a linux-based media player operating system like libreelec.
  • How to install Mathematica on Raspberry PiHow to install Mathematica on Raspberry Pi
    mathematica is an extremely powerful computational tool for processing data and math related things. in this tutorial, tipsmake.com will show you the easy steps to install mathematica onto your raspberry pi.
  • How to Install Java on LinuxHow to Install Java on Linux
    today's tipsmake will show you how to install the latest version of java runtime environment (jre) on a computer running the linux operating system.
  • How to install Asterisk on Raspberry PiHow to install Asterisk on Raspberry Pi
    asterisk is a free, open source pbx that automatically manages your calls (perfect for a small business). in this tutorial, tipsmake.com will show you how to install asterisk on a raspberry pi.
  • Instructions on 2 ways to install MongoDB on Raspberry PiInstructions on 2 ways to install MongoDB on Raspberry Pi
    in this tutorial, tipsmake will guide you through the process of installing and setting up the mongodb server software on your raspberry pi.
  • Search and launch Java Control Panel on Windows operating systemSearch and launch Java Control Panel on Windows operating system
    in java control panel you can search and change java settings. in the following article, network administrator will show you how to search for java control panel on windows operating system.