Raspberry PI Java and Javascript Desktop Applications – JJR Architecture

This entry shows how to create desktop applications for the Raspberry PI with a GUI written in JavaScript and rendered using the Java SWT Browser widget. Use this model to benefit from Rich Internet Applications and access device hardware and software resources by creating custom JavaScript functions. The assumptions are that your device is running Raspian, a Debian based Linux distribution optimised for the Raspberry PI, with X11 enabled and Java installed.

Java-JavaScript-Raspian (JJR) Architecture
The Raspberry PI is a 32 bit ARM based mini computer with built-in USB and HDMI ports, graphics card, sound, ethernet and micro SD reader. This device is capable of running Linux, RiscOS and a couple of other operating systems. With plenty of applications available for Raspian (35,000 at the time this article is written), developing your own can be an interesting hobby or a commercially viable product. Writing desktop applications for Linux the traditional way, using Qt/C++ requires a steep learning curve and a significant level of effort for creating easy to use GUIs. Writing pure Java applications can limit user interaction. Using the concept of Rich Internet Applications allows developers to use one of many JavaScript frameworks (i.e.: ExtJS, Bootstrap, jQuery UI, etc.) and create user friendly interfaces. Built on top of the Java VM, these applications can access hardware resources such as USB devices or make use of other applications. Integration between JavaScript and local resources is done by extending the JavaScript engine.

Below is a diagram of the JJR (Java-JavaScript-Raspbian) application architecture:
jjr-7218885

The Java SWT Browser Widget
Out of a couple of Java browser components (i.e.: ChromiumEmbedded or JXBrowser) the easiest to use is the Eclipse SWT Browser Widget. This component makes use of locally available browser engines, such as Mozilla or Webkit, to render web content. Coupled with the BrowserFunction class, Java functions can be invoked from within the browser using JavaScript. This component runs on MacOS and Linux – thus your application can be developed and tested on a Mac and deployed and compiled on Raspbian. SWT is a graphical widget toolkit for Java – it generates cross platform user interface components.

Requirements
Prior to compiling or executing applications developed with this widget, SWT and Webkit must be installed on your Raspbian:

sudo apt-get install -y libswt-gtk-3-jni libswt-gtk-3-jav libswt-webkit-gtk-3-jni

On Mac the required Java SWT package can be downloaded here:
http://www.eclipse.org/downloads/download.php?file=/eclipse/downloads/drops4/R-4.4-201406061215/swt-4.4-cocoa-macosx-x86_64.zip

Code summary This sample application shows how to create an SWT window, add the Browser component and load a remote web page. No IDE is required. Code is available here:

https://github.com/fgheorghe/raspberry-pi-java-and-javascript-desktop-application

Posted in DIY