<< PiJava - Part 3: What is Java FX?
PiJava - Part 1: Trying to run Java 11 on an old Raspberry PI >>

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

2019-03-13 12:31:07
TL;DR; It works! ;-) And you can even run a java file without the need to compile it.
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 Java 11

For a fresh new start, I flashed the latest Raspian OS to an SD card and created a script based on this document from Gluon to get Java 11 installed. You can get the script from my GitHub here or copy this:
# Make sure we are in the home directory
cd /home/pi

# Download the Java 11 distribution from BellSoft
wget https://github.com/bell-sw/Liberica/releases/download/jdk11%2B28/bellsoft-jdk11+28-linux-arm32-vfp-hflt.tar.gz

# Move the downloaded file to /opt
sudo mv bellsoft-jdk11+28-linux-arm32-vfp-hflt.tar.gz /opt

# Use the /opt directory
cd /opt

# Untar the downloaded file
sudo tar -xvzf bellsoft-jdk11+28-linux-arm32-vfp-hflt.tar.gz

# Remove the downloaded file
sudo rm bellsoft-jdk11+28-linux-arm32-vfp-hflt.tar.gz
To test if Java 11 is working we can simply ask the version of it.
/opt/jdk-11/bin/java --version
openjdk 11-BellSoft 2018-09-25
OpenJDK Runtime Environment (build 11-BellSoft+0)
OpenJDK Server VM (build 11-BellSoft+0, 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.
/opt/jdk-11/bin/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!