Using JavaFX on Raspberry Pi 3 with Gluon

Posted at — Aug 26, 2017
Riekpil logo
Learn how to test real-world applications with the Testing Spring Boot Applications Masterclass. Comprehensive online course with 8 modules and 130+ video lessons to master well-known Java testing libraries: JUnit 5, Mockito, Testcontainers, WireMock, Awaitility, Selenium, LocalStack, Selenide, and Spring's Outstanding Test Support.

In this post, I am going to detail the steps to get a simple JavaFX application running on the Raspberry Pi 3 using Gluon.

Hardware

For this setup, I used the Raspberry Pi 3 with the official 7" touchscreen connected.

Raspberry Pi with touchscreen

Raspbian Installation

First off, download Raspbian from https://www.raspberrypi.org/downloads/raspbian/. I chose the "RASPBIAN STRETCH WITH DESKTOP" version and burned it on an SD-card using Etcher.

Insert the SD-card in the Raspberry Pi and let it boot. The screen should look like this when done:

Raspbian start screen

Find out the IP address of the Raspberry Pi by running ifconfig on the Pi itself or looking at the DHCP clients list of your router.

Enable SSH to be able to easily copy files to the Raspberry Pi. See the official instructions on how to do that.

Installation of Java

Download the 'Linux ARM 32 Hard Float ABI' Java SE Development kit from the Oracle website. I downloaded jdk-8u144-linux-arm32-vfp-hflt.tar.gz.

Copy it to the device using SCP:

scp jdk-8u144-linux-arm32-vfp-hflt.tar.gz pi@192.168.1.60:

(Replacing 192.168.1.60 with the IP address of your own device of course)

Now unzip the copied file:

tar zxf jdk-8u144-linux-arm32-vfp-hflt.tar.gz

Move the JDK to a location of your choice. I choose /opt and also created a symbolic link for future updates:

sudo mv ./jdk1.8.0_144/ /opt/

sudo ln -s /opt/jdk1.8.0_144/ /opt/jdk8

Check if Java works by checking the version of the java executable:

pi@raspberrypi:~ $ /opt/jdk8/bin/java -version

java version "1.8.0_144"

Java(TM) SE Runtime Environment (build 1.8.0_144-b01)

Java HotSpot(TM) Client VM (build 25.144-b01, mixed mode)

Installation of JavaFX

Although Java 8 contains JavaFX by default, it does not contain what is needed to have JavaFX applications running on the Raspberry Pi. For this, you need the Gluon JavaFX Embedded SDK. Download it from http://gluonhq.com/products/mobile/javafxports/get/

Copy it to the Pi:

scp armv6hf-sdk-8.60.9.zip pi@192.168.1.60:

On the Pi itself, unzip it and copy the unzipped files onto the just installed Java SDK. Take note of what parts need to be copied where!

pi@raspberrypi:~ $ unzip armv6hf-sdk-8.60.9.zip

pi@raspberrypi:~ $ cp armv6hf-sdk/rt/lib/ext/jfxrt.jar /opt/jdk8/jre/lib/ext/

pi@raspberrypi:~ $ cp armv6hf-sdk/rt/lib/arm/* /opt/jdk8/jre/lib/arm/

pi@raspberrypi:~ $ cp armv6hf-sdk/rt/lib/javafx.platform.properties /opt/jdk8/jre/lib/

pi@raspberrypi:~ $ cp armv6hf-sdk/rt/lib/javafx.properties /opt/jdk8/jre/lib/

pi@raspberrypi:~ $ cp armv6hf-sdk/rt/lib/jfxswt.jar /opt/jdk8/jre/lib/

Running a JavaFX application

Just to get started with a very simple JavaFX application, we will take one of the javafxports samples. First clone the repository at https://bitbucket.org/javafxports/samples

Build the HelloWorld sample using

./gradlew :HelloWorld:build

Copy the created jar file to the Raspberry Pi:

scp HelloWorld/build/libs/HelloWorld.jar pi@192.168.1.60:

Now open a terminal on the Raspberry Pi itself and start the application:

sudo /opt/jdk8/bin/java -jar Helloworld.jar
The sudo is needed to make the button click work. If you know of a way to avoid the sudo part to make it work, leave a comment below!

Now you have JavaFX running on the Raspberry Pi:

Javafx on pi

There is no exit implemented in the demo application, so you have to kill it over SSH to stop it.

Conclusion

We saw how get a JavaFX application running on the Raspberry Pi 3 hardware using the Gluon JavaFX Embedded SDK. I will explain how I combined JavaFX with Spring Boot for a real fun programming stack in a future post.

This know-how originated during the development of a PegusApps project.

If you want to be notified in the future about new articles, as well as other interesting things I'm working on, join my mailing list!
I send emails quite infrequently, and will never share your email address with anyone else.