Steve On Java - JavaFX

Hacking Java, JavaFX, and Flash with Agility
  • rss
  • Home
  • NightHacking Tour
    • [Archive] NightHacking Europe – The Road to Devoxx
  • SvJugFX
  • JFXtras
    • JFXtras Individual CLA
    • JFXtras Corporate CLA
  • 2013 Travel Map
    • Let’s Meetup!
    • 2012 Travel Map
  • Contact

JavaFX on Raspberry Pi – 3 Easy Steps

steveonjava | December 18, 2012

The long awaited early access 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:

  1. Install Linux on your Raspberry Pi
  2. Download and copy Java/JavaFX 8 to your Pi
  3. 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.

Step 1 – Installing Linux on Your Raspberry Pi


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:

http://www.raspberrypi.org/downloads

To burn the image you have a few options, which are detailed here, but basically boil down to:

  • Windows: Use Win32DiskImager
  • Mac: Use the RPi-sd Card Builder
  • Linux: Use the dd command

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=720

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


Step 2 – Download and Copy Java/JavaFX 8 to Your Pi


You can download a Raspberry Pi compatible Java/JavaFX 8 build here:

http://jdk8.java.net/fxarmpreview

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 /opt

And then to run java use a command like the following:

sudo /opt/jdk1.8.0/bin/java -version


Step 3 – Deploy and Run JavaFX Apps on Your Pi


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:

https://github.com/steveonjava/LUGOD-Pi-Test

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

Here 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!

 

Share this:

  • Twitter
  • Google +1
  • More
  • Facebook
  • LinkedIn
  • Email
Categories
JavaFX, Raspberry Pi
Tags
JavaFX, linux, lugod, raspberry pi
Comments rss
Comments rss
Trackback
Trackback

« JavaOne Latin America Wrap-up Nordic NightHacking Tour Schedule »

18 Responses to “JavaFX on Raspberry Pi – 3 Easy Steps”

  1. Jim Clarke says:
    December 18, 2012 at 1:08 pm

    Tip: An iPhone charger (the Apple one) works well to supply the power. Just use it with a normal USB printer cable.

    Reply
  2. Matías says:
    December 20, 2012 at 2:49 pm

    Hi Steve, please check your mail (the one in your “contact” section). JavaFX on the Raspberry Pi is excellent!

    Reply
  3. Jens says:
    December 23, 2012 at 10:31 am

    This is awesome! Tried it today, got it up and running in 30 mins and installed Tomcat 7 and deployed a web application on Raspbian Wheezy System then is at 100% CPU for quite a while but then everything works and the performance is remarkable. Those guys must have done an incredible good job for optimizing the JVM on such a restricted hardware. Very cool!!!

    Reply
  4. CKHill says:
    December 27, 2012 at 9:11 pm

    Very very awesome!
    Thanks for sharing. I was about to ditch the raspbian wheezy image since it would only support OpenJDK so this makes my night!

    Reply
  5. Baruch Atta says:
    January 8, 2013 at 6:09 am

    You can use an adaptor cable to attach from the HDMI output of the Raspberry to a standard computer monitor. They do not say this on the Raspberry PI web site, but it is in the book (I bought it!). But this will not work on older monitors.

    Reply
    • steveonjava says:
      January 9, 2013 at 4:28 pm

      Good point. You can do this if your monitor supports HDMI input. Same caveat about plugging it in before booting so it sees a device hooked up via HDMI, and you may need to fiddle with the display resolution if your monitor does not do a good job reporting its supported formats via EDID. (more details on manual video config here: http://elinux.org/RPiconfig)

      Reply
  6. chandu0101 says:
    January 8, 2013 at 7:03 pm

    i am getting java.lang.UnsatisfiedLinkError: Can’t load library: /opt/jdk1.8.0/jre/lib/arm/libglass.so exception when running my app, full stack trace : https://forums.oracle.com/forums/thread.jspa?threadID=2483988&tstart=0

    Reply
    • steveonjava says:
      January 9, 2013 at 4:24 pm

      Did Daniel’s suggestion to set -Djavafx.platform=eglfb work? (this won’t be required in the future, but in the EA is needed right now due to http://javafx-jira.kenai.com/browse/RT-27194)

      Reply
  7. chandu0101 says:
    January 19, 2013 at 8:30 am

    yes, there is a typo in my command :( , i am missing letter ‘m’ in platform . anyway here is my frist JavafX app running on Raspberry pi http://www.youtube.com/watch?v=Rkidpcag1hc :)

    Reply
    • steveonjava says:
      January 20, 2013 at 1:46 am

      Very cool!

      Reply
  8. lifematch says:
    February 3, 2013 at 6:27 pm

    What directory do you need to be in order to run your java code?

    Reply
  9. lifematch says:
    February 3, 2013 at 6:56 pm

    mouse does not work?

    Reply
    • steveonjava says:
      February 3, 2013 at 11:42 pm

      The mouse works with JavaFX. There are some issues with USB device ordering, so try unplugging other USB devices (or a different mouse if it appears like multiple devices to the OS. For debugging try in xwindows first and read the startup dmsg log.

      Reply
  10. Mathieu says:
    April 2, 2013 at 2:36 am

    Hi Steve,

    I’m Mathieu, we met at Devoxx France. I ran my first attempt at getting Java FX to display the temperature from an Arduino on the Raspberry Pi, using serial communications directly on the serial port. The hardware part went well, the Pi got the Arduino powered up, and serial port started getting data readings.

    On the software end of the problem, I do not know what happened: I tried running my JavaFX application from the jar file. It is pretty basic: I use a serial port callback provided by Pi4J library, and this callback writes data directly to a label. When I run it from command line, it waits a bit, and gives up, without any message.

    I will double check my install, I may have missed something (two things come to my head currently: I ran the command from SSH, so the display may be wrong (like missing ‘export DISPLAY’ while doing some X remoting) and I didn’t check my video RAM (might be 16Mb…))

    Currently, here is what I have for Arduino plugged to Mac Book using FTDI cable and Processing environment:
    https://gist.github.com/mathieubolla/5246609#file-arduinothermosensor

    I will keep you informed of the outcome of my tests…

    Reply
  11. Mathieu says:
    April 2, 2013 at 2:58 pm

    Hi Steve,

    I finally got it working. It was the memory split indeed. I had 16Mb, now have 128Mb for video. Plus the library pi4j is not correctly packaged, it has some duplicates in jar files which didn’t pass jar signing process.

    Here is my git hub with all required stuff to run my proof of concept: https://github.com/mathieubolla/pihacking

    And some pictures to show it working live:
    https://twitter.com/mathieubolla/status/319194833476546560/photo/1

    Plus a zoom on the ugly UART connection:
    https://twitter.com/mathieubolla/status/319195264248340480/photo/1

    Thanks again for your inspiring presentation, and for all the JavaFX work on Raspberry Pi. Definitely something to explore again and again…

    Cheers, from France.

    Mathieu

    Reply
    • steveonjava says:
      April 2, 2013 at 6:49 pm

      Awesome! That is a nice project showing serial communication in Java on Raspberry Pi, thanks for sharing the code!

      Reply
  12. Reedyseth says:
    April 3, 2013 at 5:36 pm

    The tutorial is well commented, but what if I whant to run java SDK 1.3.0_02 on a Rasperry Pi Soft-float Debian “wheezy”, have you ever tried to do this, I been working on it and I cant make it work, I made run a newer version but not this one, can you give a little advice please, thank you.

    Reply
    • steveonjava says:
      April 3, 2013 at 7:40 pm

      Not sure how much luck you are going to have with something as old as 1.3. If you want to do a soft float build, you can try the Java 7 SE Embedded builds here: http://www.oracle.com/technetwork/java/embedded/downloads/javase/index.html

      Remember that these will be headless (no GUI) and you will be taking a performance hit by not using the FPU.

      Reply

Leave a Reply

Click here to cancel reply.

  • Travel Map - Let's Meetup

Publications

  

Affiliations

Awards

2009/2011 JavaOne Rock Star!

Disclaimer

Views and opinions expressed here are all my fault... complain to me, not my employer. :)
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox
loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.