Friday Oct 25, 2013

Integrating NetBeans for Raspberry Pi Java Development

Raspberry Pi IDE Java Development The Raspberry Pi is an incredible device for building embedded Java applications but, despite being able to run an IDE on the Pi it really pushes things to the limit.  It's much better to use a PC or laptop to develop the code and then deploy and test on the Pi.  What I thought I'd do in this blog entry was to run through the steps necessary to set up NetBeans on a PC for Java code development, with automatic deployment to the Raspberry Pi as part of the build process.

I will assume that your starting point is a Raspberry Pi with an SD card that has one of the latest Raspbian images on it.  This is good because this now includes the JDK 7 as part of the distro, so no need to download and install a separate JDK.  I will also assume that you have installed the JDK and NetBeans on your PC.  These can be downloaded here.

There are numerous approaches you can take to this including mounting the file system from the Raspberry Pi remotely on your development machine.  I tried this and I found that NetBeans got rather upset if the file system disappeared either through network interruption or the Raspberry Pi being turned off.  The following method uses copying over SSH, which will fail more gracefully if the Pi is not responding.

Step 1: Enable SSH on the Raspberry Pi

To run the Java applications you create you will need to start Java on the Raspberry Pi with the appropriate class name, classpath and parameters.  For non-JavaFX applications you can either do this from the Raspberry Pi desktop or, if you do not have a monitor connected through a remote command line.  To execute the remote command line you need to enable SSH (a secure shell login over the network) and connect using an application like PuTTY.

You can enable SSH when you first boot the Raspberry Pi, as the raspi-config program runs automatically.  You can also run it at any time afterwards by running the command:

sudo raspi-config

This will bring up a menu of options.  Select '8 Advanced Options' and on the next screen select 'A$ SSH'.  Select 'Enable' and the task is complete.

Step 2: Configure Raspberry Pi Networking

By default, the Raspbian distribution configures the ethernet connection to use DHCP rather than a static IP address.  You can continue to use DHCP if you want, but to avoid having to potentially change settings whenever you reboot the Pi using a static IP address is simpler.

To configure this on the Pi you need to edit the /etc/network/interfaces file.  You will need to do this as root using the sudo command, so something like sudo vi /etc/network/interfaces.  In this file you will see this line:

iface eth0 inet dhcp

This needs to be changed to the following:

iface eth0 inet static

You will need to change the values in red to an appropriate IP address and to match the address of your gateway.

Step 3: Create a Public-Private Key Pair On Your Development Machine

How you do this will depend on which Operating system you are using:

Mac OSX or Linux

Run the command:

ssh-keygen -t rsa

Press ENTER/RETURN to accept the default destination for saving the key.  We do not need a passphrase so simply press ENTER/RETURN for an empty one and once more to confirm.

The key will be created in the file .ssh/ in your home directory.  Display the contents of this file using the cat command:

cat ~/.ssh/

Open a window, SSH to the Raspberry Pi and login.  Change directory to .ssh and edit the authorized_keys file (don't worry if the file does not exist).  Copy and paste the contents of the file to the authorized_keys file and save it.


Since Windows is not a UNIX derivative operating system it does not include the necessary key generating software by default.  To generate the key I used puttygen.exe which is available from the same site that provides the PuTTY application, here.

Download this and run it on your Windows machine.  Follow the instructions to generate a key.  I remove the key comment, but you can leave that if you want.


Click "Save private key", confirm that you don't want to use a passphrase and select a filename and location for the key.

Copy the public key from the part of the window marked, "Public key for pasting into OpenSSH authorized_keys file".  Use PuTTY to connect to the Raspberry Pi and login.  Change directory to .ssh and edit the authorized_keys file (don't worry if this does not exist).  Paste the key information at the end of this file and save it.

Logout and then start PuTTY again.  This time we need to create a saved session using the private key.  Type in the IP address of the Raspberry Pi in the "Hostname (or IP address)" field and expand "SSH" under the "Connection" category.  Select "Auth" (see the screen shot below).


Click the "Browse" button under "Private key file for authentication" and select the file you saved from puttygen.

Go back to the "Session" category and enter a short name in the saved sessions field, as shown below.  Click "Save" to save the session.


Step 4: Test The Configuration

You should now have the ability to use scp (Mac/Linux) or pscp.exe (Windows) to copy files from your development machine to the Raspberry Pi without needing to authenticate by typing in a password (so we can automate the process in NetBeans).  It's a good idea to test this using something like:

scp /tmp/foo pi@

on Linux or Mac or

pscp.exe foo pi@raspi:/tmp

on Windows (Note that we use the saved configuration name instead of the IP address or hostname so the public key is picked up). pscp.exe is another tool available from the creators of PuTTY.

Step 5: Configure the NetBeans Build Script

Start NetBeans and create a new project (or open an existing one that you want to deploy automatically to the Raspberry Pi).

Select the Files tab in the explorer window and expand your project.  You will see a build.xml file.  Double click this to edit it.


This file will mostly be comments.  At the end (but within the </project> tag) add the XML for <target name="-post-jar">, shown below


Here's the code again in case you want to use cut-and-paste:

<target name="-post-jar">
  <echo level="info" message="Copying dist directory to remote Pi"/>
  <exec executable="scp" dir="${basedir}">
    <arg line="-r"/>
    <arg value="dist"/>
    <arg value="pi@"/>

For Windows it will be slightly different:

<target name="-post-jar">
  <echo level="info" message="Copying dist directory to remote Pi"/>
  <exec executable="C:\pi\putty\pscp.exe" dir="${basedir}">
    <arg line="-r"/>
    <arg value="dist"/>
    <arg value="pi@raspi:NetBeans/CopyTest"/>

You will also need to ensure that pscp.exe is in your PATH (or specify a fully qualified pathname).

From now on when you clean and build the project the dist directory will automatically be copied to the Raspberry Pi ready for testing.

Friday Oct 04, 2013

JavaOne Afterglow

JavaOne Afterglow Last week was the eighteenth JavaOne conference and I thought it would be a good idea to write up my thoughts about how things went.

Firstly thanks to Yoshio Terada for the photos, I didn't bother bringing a camera with me so it's good to have some pictures to add to the words.

Things kicked off full-throttle on Sunday.  We had the Java Champions and JUG leaders breakfast, which was a great way to meet up with a lot of familiar faces and start talking all things Java.  At midday the show really started with the Strategy and Technical Keynotes.  This was always going to be tougher job than some years because there was no big shiny ball to reveal to the audience.  With the Java EE 7 spec being finalised a few months ago and Java SE 8, Java ME 8 and JDK8 not due until the start of next year there was not going to be any big announcement.  I thought both keynotes worked really well each focusing on the things most important to Java developers:


One of the things that is becoming more and more prominent in many companies marketing is the Internet of Things (IoT).  We've moved from the conventional desktop/laptop environment to much more mobile connected computing with smart phones and tablets.  The next wave of the internet is not just billions of people connected, but 10s or 100s of billions of devices connected to the network, all generating data and providing much more precise control of almost any process you can imagine.  This ties into the ideas of Big Data and Cloud Computing, but implementation is certainly not without its challenges.  As Peter Utzschneider explained it's about three Vs: Volume, Velocity and Value.  All these devices will create huge volumes of data at very high speed; to avoid being overloaded these devices will need some sort of processing capabilities that can filter the useful data from the redundant.  The raw data then needs to be turned into useful information that has value.  To make this happen will require applications on devices, at gateways and on the back-end servers, all very tightly integrated.  This is where Java plays a pivotal role, write once, run everywhere becomes essential, having nine million developers fluent in the language makes it the defacto lingua franca of IoT.  There will be lots more information on how this will become a reality, so watch this space.


How do we make the IoT a reality, technically?  Using the game of chess Mark Reinhold, with the help of people like John Ceccarelli, Jasper Potts and Richard Bair, showed what you could do.  Using Java EE on the back end, Java SE and JavaFX on the desktop and Java ME Embedded and JavaFX on devices they showed a complete end-to-end demo.  This was really impressive, using 3D features from JavaFX 8 (that's included with JDK8) to make a 3D animated Duke chess board.  Jasper also unveiled the "DukePad" a home made tablet using a Raspberry Pi, touch screen and accelerometer.  Although the Raspberry Pi doesn't have earth shattering CPU performance (about the same level as a mid 1990s Pentium), it does have really quite good GPU performance so the GUI works really well.  The plans are all open sourced and available here.  One small, but very significant announcement was that Java SE will now be included with the NOOB and Raspbian Linux distros provided by the Raspberry Pi foundation (these can be found here).  No more hassle having to download and install the JDK after you've flashed your SD card OS image.  The finale was the Raspberry Pi powered chess playing robot.  Really very, very cool.  I talked to Jasper about this and he told me each of the chess pieces had been 3D printed and then he had to use acetone to give them a glossy finish (not sure what his wife thought of him spending hours in the kitchen in a gas mask!)  The way the robot arm worked was very impressive as it did not have any positioning data (like a potentiometer connected to each motor), but relied purely on carefully calibrated timings to get the arm to the right place.  Having done things like this myself in the past I know how easy it is to find a small error gets magnified into very big mistakes.

Here's some pictures from the keynote:


The queue to get in.  Back at the Moscone for the keynote this year, which was nice.


The "Dukepad" architecture


Nice clear perspex case so you can see the innards.


The very nice 3D chess set.  Maya's obviously a great tool.


The robotic chess player.

After the keynotes it was sessions, hands on labs, BoFs and parties for the next four days.  Here's a few highlights:

  • Anything Lambda related was packed.  Good to see that there's lots of interest and people are really keen to use this great new feature.  For me, the real power is in the changes to the libraries that use the Stream and related classes.  I helped run the Lambda programming Hands on Lab.  If you're interested Stuart Marks has posted the materials on his blog.
  • My session on the Raspberry Pi JavaFX Carputer went really well.  Since I couldn't bring my car with me I'd made a short video of the system in action.  It was one of those rare occasions when I new that my demo would work!  I also managed to get my simulator working while I was at JavaOne so was able to show data recorded from a real run being played back on my device.  There will be more blog entries to follow on this shortly.
  • My other session was on JavaFX with the Leap Motion controller.  Thankfully for this I had the expert help of Gerrit Grunwald, Johan Voss and José Pereda who came with some great demos to complement my rather basic ones.  During the week I was lucky enough to go and visit Leap Motion, who are based in San Francisco and talk about some of the great stuff they're doing to make the controller even better.
  • The Java leaders visit to the baseball game was fun (unless you're a Giants fan).  Not totally convinced about baseball, but then compared to cricket, it's actually quite a fast paced game.
  • I didn't go to the appreciation event this year on Treasure Island.  The idea of queuing for a bus for an hour to get a free burger and beer and listen to Maroon 5 was less appealing than a quiet dinner with my colleagues (and a bit of a break from the non-stop Java action).
On the last day it was the Community Keynote, which was the highlight of the week for me (watch it here).  It's always great to celebrate the way that community makes Java different to other programming languages, but this year the organisers excelled themselves.  James Gosling was back at JavaOne again, talking more about Liquid Robotics and showing some of the exciting things he's doing with JavaFX.  My favorite quote was when he came on stage and said, "I guess NetBeans is the new PowerPoint", a reference to the fact that most of the presenters had eschewed slides in favour of code and they'd all used NetBeans. (if you haven't done so yet you should really try out the 7.4 release candidate)  Stephen Chin was also on stage to show a LEGO robot Duke that he'd built and worked as a Segway using the recently released Mindstorms EV3 kit and the Java SE environment.  One sad piece of news related to this is that later in the day, Stephen, Angela Caicedo and I went for a cup of coffee and Angela's car was broken into.  All that was stolen was a bag containing Duke.  If you see him in a downtown San Francisco dumpster make sure he gets home.

By far the best part of the keynote was where my good friend Arun Gupta's son, Aditya, got up on stage and showed over 1500 people how to hack Minecraft.  As a presenter he was flawless, he seemed confident, his demos worked and he presented the concepts clearly and with great demos.  Hard enough for a seasoned presenter, but consider that Aditya is only TEN YEARS OLD!  There's no way I could have done that at his age.  He deserves major respect for this, which is probably why he got a standing ovation when he finished.

You can watch the video of his performance here.

So that was JavaOne 2013.  Another great event and it will be even harder to top that next year.  One challenge I have taken away from this is that my son, Dylan, is only 7 years old.  I have less than three years to get him on stage talking about Java during the keynote at JavaOne!


A blog covering aspects of Java SE, JavaFX and embedded Java that I find fun and interesting and want to share with other developers. As part of the Developer Outreach team at Oracle I write a lot of demo code and I use this blog to highlight useful tips and techniques I learn along the way.


« October 2013 »