How to Install Java on Raspberry Pi?
Java is one of the best computer programming languages that you can use to build numerous applications on your system. Various web-based applications are built with Java just like the popular Minecraft video game. There are two types of Java — Oracle Java and OpenJDK. You can install Java on your Raspberry Pi as well to build various types of software for your personal or organizational use. If you want to install Java on Raspberry Pi, you can easily do so with the help of a few commands. We will get into that shortly but before that, let us tell you something about Oracle Java and OpenJDK.
Well, Oracle Java offers a license that helps developers with the non-commercial use of the computing language. On the other hand, OpenJDK is an open-sourced implementation of Java that allows you to use the platform for commercial uses as well. Now, the Raspbian repositories already have two different Java packages which are Java Runtime Environment (JRE) and Java Development Kit (JDK). In JRE, there is Java virtual machine (JVM), programs, and binaries that help developers to run programs with Java.
Within JDK, there is JRE along with many other developments and repairing tools for Java that helps building web applications with Java language. If you are wondering what type of Java you should use on Raspberry Pi, we recommend you go with the JDK type. But sometimes, the version of Java you require to build an application depends on the nature of the application. So, before you decide which version of Java will be suitable for you, check out the requirements for the application.
In this article, we are going to describe how to install Java OpenJDK on Raspberry Pi because that’s the most recommended version. If you need any other version of Java, you may read our other articles.
Check out these prerequisites before installing Java on your Raspberry Pi:
- You will require a Raspberry Pi 2, 3, or 4.
- You should be able to access the terminal or command line.
- A user account with Sudo privileges is a must-have.
If you have these accesses, move on to the next section to learn how to install Java on Raspberry Pi.
Installation Method of Java on Raspberry Pi
Raspberry Pi is equipped with the Raspbian operating system and the device is based on Debian. So, you can install Java with the apt package manager. Before we proceed with the installation, we recommend you update all the software packages from the Raspbian software repositories so that they can be compatible with OpenJDK. This guide was tested on the latest version of Raspbian aka buster. But it should work on any older version.
To update the existing packages in Raspberry Pi, run the following commands:
sudo apt update
sudo apt upgrade
The update will take some time depending on your Internet connection and the package volume. As soon as the update is executed, you can install the latest version of Java on your Raspberry Pi. Use this command to install Java on Raspberry:
sudo apt install default-jdk
Type Y and hit the Enter key to complete the installation (if the prompt appears).
When the installation is done, verify the version of Java with this command:
Your output will be 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)
This output indicates that you have successfully installed Java OpenJDK on your Raspberry Pi. And by now, Java will be up and running on your system.
Important: If you have installed Java 11 first before installing Java 8 version on your system, it will show you the Java 11 instance version because your system always uses the latest and updated version of Java. But if you want to use Java 8 and not 11, you can set up the default version of Java on your system by yourself. Check out the next system to discover how it is done.
How to Set Up the Default Java Version?
If your web-based application requires Java LTS version 8 to run on your system, then you can install it with a few commands as well. Install Java 8 with this command:
sudo apt update
sudo apt install openjdk-8-jdk
Once the installation is done, verify it using
The output will be something similar to 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)
Now if your system has two different versions of Java, then you will have to set one as default. Check the current Java version which is probably the latest version of Java on your system with this command:
Run the following command to set another default Java version which is Java 8:
sudo update-alternatives –config java
The output that follows will show you all the installed Java instances on your Raspberry device:
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:
The default version of Java will have the * sign beside it. You can enter the number of the version of Java that you want as default and hit the Enter key. The Java version you want as the default will be successfully saved. But if you want to change or set up the location in your Raspberry Pi where you want the Java installation to be done, you can check out the following section.
Optional: Set the JAVA_HOME Path Environment Variable
If you want, you can set the JAVA_HOME path environment variable manually on your Raspberry device. Check out the location of your Java version with this command:
sudo update-alternatives –config java
Usually, the locations of Java 11 and Java 8 on Raspberry devices are as follow:
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
Once you find the location of the default Java version, edit the /etc/environment file with the following command:
sudo nano /etc/environment
Add this to the file: JAVA_HOME=”<em>path_of_your_default_version_of_java</em>”
In place of “of_your_default_version”, you should insert “java-8-openjdk-armhf/jre/bin/”, for example.
Now save the file and exit the editor. And reboot your Raspberry with this command: sudo reboot. Once your system restarts, it should make the changes. To verify the changes, run this command: echo $JAVA_HOME
The previous section demonstrates how you can change the JAVA_HOME Path Environment Variable in Raspberry Pi. But note that the etc/environment file is a configuration file and whatever you change in it will affect the user on your system. So, you have to keep that in mind before making any changes to the variable file. Also, if you want to uninstall Java from your Raspberry Pi, you can run sudo apt remove openjdk-8-jdk for Java 8 and sudo apt remove default-jdk for Java 11 on your system.
Differently, the whole article has shown you how to install Java on Raspberry Pi with the help of just a few commands. Sometimes during installation and uninstallation of Java, your device wants you to confirm the process by pressing Y and the Enter key. Keep an eye on that prompt to complete the installation process. If you need any help with installing Java on Raspberry Pi, make sure to drop us a comment below or check out our other articles.