JavaFX on Raspberry Pi – 3 Easy Steps
Join the DZone community and get the full member experience.Join For Free
The long awaited developer preview of JavaFX on Raspberry Pi is finally out. This is a great platform for doing small embedded projects, a low cost computing system for teaching, and great fun for hobbyists. It only costs $35 for the Model B version with 512MB RAM, 700MHz ARM processor and I/O for HDMI, Composite, Audio, Ethernet, and 2 USB ports.
So what can you do with JavaFX on a Raspberry Pi? A great example is the digital signage that we put together for Devoxx showing the conference schedule flying by on animated space ships:
Update: And if you are not convinced that Java is performant on the Pi, check out Rich Bair’s post on fxexperience.com: http://fxexperience.com/2012/12/javafx-on-raspberry-pi/
So if you are convinced to get started, there are 3 easy steps to get JavaFX running on your Pi:
- Install Linux on your Raspberry Pi
- Download and copy Java/JavaFX 8 to your Pi
- Deploy and run JavaFX apps on your Pi
This does assume you have a Pi… For questions about where to get a
Pi, how to power it, etc., I would recommend checking out the Raspberry Pi Site.
The latest Java 8 release is hard float, which is a good thing, because it gives you better performance and the recommended Raspberry Pi build is also hard float. Stay away from anything that says soft, softfp, etc., because it will be incompatible with the hard float JVM.
Note: Why all this fuss over floating point? Well, low power embedded systems skip the floating point hardware to save cost (e.g. ARM Cortex M0-M3). Fortunately the ARMv6 chip used in the Raspberry Pi has real floating point support.
To setup Linux on your SD card, you will need a Windows, Mac, or Linux desktop with an SD Card Reader/Writer. The distribution you want is the latest Raspbian Wheezy hard float build, which is the recommended install on the Raspberry Pi download site:
To burn the image you have a few options, which are detailed here, but basically boil down to:
Once your SD card is created, take your Raspberry Pi out of the box, pop in the SD Card, hook it up to a monitor or TV lying around your place, and plug it up to a nice usb power brick (5V 700mA or greater).
Warning: Order Matters! – If you don’t hook up an HDMI monitor before powering on the Pi, it assumes composite. This means if the lights are flashing but you get a blank screen, you should try rebooting the Pi (by unplugging and replugging it in).
At this point, your Pi should bootup and show you the Raspberry Pi config screen. There are some things you might want to consider tweaking here, including:
- CPU/GPU memory split – Give the GPU at least 128MB of ram so graphical heavy apps will run better (important for JavaFX!)
- Change the locale/keyboard/timezone – Default settings are for the UK, so everyone else should change these or you will be cursing at your keyboard when trying to type punctuation!
- Overscan – If your display has black bars around the edges, turn this off so you can use the full resolution.
- Expand root filesystem – It can do an online resize of your card to use the full space (default image has a tiny 2GB root partition). Highly recommended, but expect this to take a while on a large card.
- SSH – Turn this on if you want to access your Pi over the network (this is the only way to shut down rogue JavaFX processes short of rebooting)
In the official JavaFX Raspberry Pi docs they also recommend hacking the framebuffer to be 720p in the config file (/boot/config.txt) by uncommenting these lines:
framebuffer_width=1280 framebuffer_height=720This is not strictly required, but will give you better performance since the Pi is pushing fewer pixels. If you are going to do this, you might also want to force the Pi to run in 720 resolution as well to avoid pixel upscaling. Don’t try hacking the display settings unless you know what you are doing (and have an ssh terminal to login remotely in case you kill the display). For more info about the HDMI display nodes, check out the docs here.
You can download a Raspberry Pi compatible Java/JavaFX 8 build here:
If your Pi is hooked up to the network via ethernet you can download it directly to the device. Otherwise copy it over using sftp (via ssh) or sneakernet (a USB key).
Once you have it downloaded, you can unzip it to a location of your choice:
sudo tar -zxvf file_name -C /optAnd then to run java use a command like the following:
sudo /opt/jdk1.8.0/bin/java -version
Almost any JavaFX desktop application will run on the Pi simply by copying over the jar file and executing it locally, with no modifications. (The 2 exceptions to this are applications that rely on WebView or MediaView, both of which are unimplemented in the current dev preview)
A great way to get started building JavaFX applications for the Pi is to use Scene Builder to quickly put together a user interface visually, and then deploy that to the Pi. I did this last night at the Linux Users’ Group of Davis (LUGOD), quickly putting together a sample application with help from the very astute attendees (I also learned a thing or two about linux command tricks along the way from the audience).
I posted our 15 minute application in GitHub as an example you can try:
To run it on the Pi, build the source with your favorite IDE (or straight form the command line) and build a jar file. Then copy the jar file to your Pi and run it with a command like the following:
sudo /opt/jdk1.8.0/bin/java -Djavafx.platform=eglfb \ -cp /opt/jdk1.8.0/jre/lib/jfxrt.jar:LUGODTest.jar lugodtest.LUGODTestHere is a picture of the Scene Builder project and the output we got on the Raspberry Pi during the LUGOD meeting:
However, the most impressive demo is your own. Try deploying your own JavaFX applications and leave others pointers to apps you have deployed in the comments below!
Published at DZone with permission of Stephen Chin, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.