Install Java 11 on the Raspberry Pi

This tutorial teaches you how to run and install Java 11 on the Raspberry Pi. We will install, create a very simple “Hello World” application as well as compile that program and run in on the Raspberry Pi.

You find more information about Java here

Introduction

Java is one of the most popular programming languages in the world and with some simple command it is available to you on the Raspberry Pi.

Prerequisites

First of all I think it is always a good idea to have your Raspberry Pi up to date. You do that by running the following command in the terminal. It will update and upgrade all the programs you currently have installed on your device. It will try to update quite a few libraries and programs and you need to enter yes when upgrading.

# sudo apt-get update && apt-get upgrade

When everything is updated you want to install the Java packages. You do that by running the following command. (It will take some time, a lot of things are going to be installed.)

Install

# sudo apt-get install openjdk-java11

When the Raspberry Pi has installed the Java packages you should be able to run ”java -version” and have the following output:

Compile

Also the java compiler should be present, try running ”javac –version” and you should have a similar printout like this:

You have now installed Java on your Raspberry Pi! But how do we run a program? Let’s create a very simple program to demonstrate this. I am using a program called nano to write a very simple application, also known as ”Hello World”.

# nano HelloWorld.java

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World");
    }
}

Ctrl + X and yes, to save and exit Nano.

Run

We have now only the source code for the program. To be able to run the program you want to compile the program and that is what the program ”javac” is for. Run the following command to compile the Hello World program.

# javac HelloWorld.java

You should now be able to see a new file called HelloWorld.class appear in the same folder as HelloWorld.java. That is the compiled program for the HelloWorld.java.

You can now run your program using the following command.

# java HelloWorld

Summary

And there you go, you have now installed Java 11 and your first java application is running on your Raspberry Pi. Good work!

What’s next?

Java is one of the most popular languages in the world and a lot of different frameworks exists which will make your life easier when programming Java. One of them is Spring, I suggest checkout out this tutorial to getting started with Spring Boot. You can also see this Philips Hue tutorial to get inspired!