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.


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 »