PiJava - Part 2 - Installing Java 11 on a Raspberry PI 3 Model B+

After my first (and failed) attempt to get Java 11 running on an old Raspberry PI, I bought a brand new 3 Model B+ version.

Installing a recent Java JDK which includes JavaFX

UPDATE April 30, 2020
An earlier version of this blog showed how to install Java 11 on the Raspberry Pi, but Raspberry Pi OS has this now already included! So you can immediately run a Java file, or follow the update steps below to install a newer JDK which includes JavaFX. Read more on this page “Installing Java and JavaFX on the Raspberry Pi”

For a fresh new start, flash the latest FULL Raspberry Pi OS to an SD card. To upgrade the included Java JDK to a newer version with JavaFX follow these steps, or use one of the scripts you can find on the code respository of my book:

# Make sure we are in the home directory
cd /home/pi

# Download the Java 11 distribution from BellSoft
wget http://download.bell-sw.com/java/14+36/bellsoft-jdk14+36-linux-arm32-vfp-hflt-full.deb

# Install the downloaded SDK
sudo apt-get install ./bellsoft-jdk14+36-linux-arm32-vfp-hflt-full.deb

# Update the link to javac
sudo update-alternatives --config javac

# Update the link to java
sudo update-alternatives --config java

To test if Java is working we can simply ask the version of it in the terminal.

$ java --version
openjdk version "14" 2020-03-16
OpenJDK Runtime Environment (build 14+36)
OpenJDK Server VM (build 14+36, mixed mode)

Running our first Java application

Thanks to a new feature in Java 11 we can run a Java file without compiling it, which is very handy just to test Java itself on the PI. See this link for a full article describing this more in detail. In short:

… Well, for the first time in Java history, we will know longer need the ceremony associated with compiled languages of compiling/linking and then running executable code. Now, one can run their code simply through invoking the java command! …

So we start with creating the base of all applications, a HelloWorld example! You can get it also directly from my GitHub here or copy this:

cd /home/pi
nano HelloWorld.java

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

We can now run this file directly from the terminal.

java /home/pi/HelloWorld.java
Hello World

Conclusion

If you have a recent PI and you know which Java version you need and how to get it on the board (preferably with a script), you can have a running Java application in minutes on your PI!