Friday, July 19, 2013

Give the Nashorn some Pi...

Hi there,

Exciting times...since 2 weeks we have an updated version of JDK8 available for the Raspberry Pi and it's based on build b97 (well in the meantime we have b98 and b99 should be available soon). This means the restrictions that we had with the december build of JDK8 for the Pi are gone...e.g. custom controls that have been developed for the desktop will run on the Pi without modification...YAY!!! Finally I can test my standard Enzo build on the Pi which is awesome.
In addition to this we now also have 3D support and Nashorn on the Pi...and again YAY!!!
Now it's time to give the Nashorn some Pi and see if it works. I explained my idea some weeks ago, if you develop Java for the Raspberry Pi it's not that bad, you can develop your application on your desktop computer, create a jar, copy the jar to the Pi and start it there. That's ok...BUT if you would like to change something or test something this procedure could get boring because it's timeconsuming. So one possible solution might be using property files that you load at startup and so you have the chance to modify parameters. The disadvantage is that you have take care about each property in your app which means you have to know all the parameters you would like to change up front...not a really good solution.
When I read about Nashorn the idea came to my mind that it might be cool if I could access JavaFX controls from a script that could be loaded at runtime, so not only at startup but also when the application is running.
Well I tested that and it worked on the desktop so now it's time to test it on the Raspberry Pi.
In principle you could use the same code that I use for the "Taming the Nashorn again..." blog post to run a script at startup and you will figure out that it simply works.

You have to put the nashorn.jar which is in $JAVA_HOME/jre/lib/ext on the classpath (don't ask me why but Jim Laskey figured that out and it worked...thanx to Jim for the hint).

To load a script during runtime you can use the same routine as for loading it at startup, so that's easy but I would like to go a step further, add a Filesystem Listener to a javascript file and reload the file if it was modified. With this approach I would be able to start a JavaFX application on the Raspberry Pi, connect to the Pi via SSH and modify the JavaScript file with a simple text editor like nano on the shell. After I save the JavaScript file the JavaFX application will be triggered because the file was modified and so the file will be reloaded and re-applied to the application (make sure that you are on the FXApplicationThread by calling Platform.runLater(new Runnable())...).
Because the polling of the Filesystem Listener is not that fast it could take a second or two before the Application will change but this is no problem because I can now modify a running application...sweet.
Ok, that's nice but what if I don't have a direct network connection to my Raspberry Pi? Means I can't use SSH easily to connect to the Pi and modify the JavaScript file...
Well as you might know I'm a big fan of XMPP so why not using a simple chat to send the script directly to the JavaFX application on the Raspberry Pi and evaluate it by the Nashorn scripting engine. Long story's just to easy to implement because you simply send a text message to the JavaFX application via XMPP and pass the script text to the Nashorn scripting engine where it will get interpreted...done :)
To give you an idea about how that works I've created a little screen video that I've put on YouTube.

To see the text in the shell window you should increase the screen size of the video.

I won't show any code here because it's not finished yet so please be give me some time to prepare a little project that will contain the code...

I've put the code I used for the demo on a BitBucket repo, so feel free to take a look at it...

That's it for today...enjoy taming the Nashorn and keep coding...

No comments:

Post a Comment