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

UPDATE 03/09/2019: as you can see in the Raspbian release notes the default Java version in Raspbian OS for the Pi has been replaced by OpenJDK 11 on 20/06/2019. So you will be able to run the HelloWorld example without the need of a new Java version. But to run the JavaFX application in the following parts you will still need to install the BellSoft Liberica JDK which has JavaFX included.
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 Raspbian 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!


Related posts

ENPiJava overview: Java 11 and JavaFX 11 on Raspberry PI
As my daily work mainly is Java and back-end stuff on "real servers", I set myself for 2019 as a personal goal to experi...

ENPiJava - Part 6: JavaFX 11 application on Raspberry PI with TilesFX and GPIO
What we will do Based on the previous blog posts we can install and run Java 11 we have a minimal JavaFX 11 applica...
ENPiJava - Part 5: Running the minimal JavaFX 11 application on Raspberry PI
In part 2 of this blog series Java 11 was successfully installed on a PI. And in part 4 a running Java FX jar was gen...
ENPiJava - Part 4: Building a minimal JavaFX 11 application with Maven
I prefer a Java app above a web app, because starting a new "modern" web development requires you to pull a bunch of dep...
ENPiJava - Part 3: What is Java FX?
What is JavaFX? From the Oracle site: "JavaFX is a set of graphics and media packages that enables developers to design...
ENPiJava - Part 2: Installing and running 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+ versi...
ENPiJava - Part 1: Trying to run Java 11 on an old Raspberry PI
One of my goals for 2019 is experimenting with the latest Java and JavaFX versions on a Raspberry PI. After my experimen...