Java Apps on the Raspberry Pi
I've been working on a few smaller projects (pun intended) involving Java SE Embedded and the Raspberry Pi. For those of you new to this platform, the Pi is an educational computer created to foster computer software and hardware development, aimed specifically at kids. Don't let its size fool you, it's a full-fledged computer complete with a desktop UI. To see just how small it is, this photo shows a Pi on a full-sized iPad.
Be sure to check out this page for the full history and inspiring story behind the Pi. It's an entire system on a chip, complete with 512MB of RAM, an ARMv6 CPU capable of running at 1GHz, two USB ports, an ethernet port, an HDMI and composite video port, and various I/O ports to serve as a controller for other devices, all for around $35.
The Raspberry Pi site contains stories with videos of school-age people building fantastic (and truly useful) projects with their Pi. But it doesn't end with kids. Hobbyists, teachers, and others interested in making the most of cool and affordable technology have done some interesting things with the Pi. And that includes me. I've been experimenting with Java on the Pi for the past few months, using it to control an Arduino via the serial (USB) port, among other experiments. Let's see what it takes to get up and running with Java on a Raspberry Pi. If you already have a Pi working, you can skip to the section titled "Upgrading Your OS Image" below. Don't skip that section, especially if your Pi has 512MB of RAM.
Setting Up Your Pi
After you acquire a Pi — I ordered mine through Amazon — you'll need to purchase an SD card to serve as local disk. Caution: Be careful which SD card you buy. Not only does it need to be one of the known cards that work with the Pi, you want to be sure to get one with enough space to hold the software you need, that it's fast enough so boot up and operation times are acceptable, and that the price is reasonable. Size matters if you want to install software such as Java, additionally libraries, a Java Servlet engine or embedded application server, or even a database. You can find a list of acceptable SD cards of all sizes, speeds, and manufactures here. I chose a SanDisk 32GB Ultra (30MB/s) class 10 SDHC card. This card provides good storage space with good performance, at a reasonable price ($25 to $50 depending on where you get it).
Next, you'll need to get an OS image and write it to the card. To do this, follow these steps:
- Download the right version of the OS for Java to work. Currently, Java requires software-based floating-point support (also called SoftFP). You can download the soft-float version of Debian "Wheezy" here.
- "Burn" the OS image to the SD card from your host computer, which can be a desktop or laptop running Windows, Linux, or Mac OS X. Instructions are available, but personally I found that the RPI-sd card builder for Mac OS X (worked best on my Mac, although the command-line works as well. For Windows, the best choice is Win32DiskImager. For Linux, there's ImageWriter or the command-line. If you run into problems, or you just don't care to spend your time burning OS images, you can purchase SD cards with a Raspberry Pi Linux OS already on it.
Once your SD card is ready, place the card into the card slot on the back of your Pi (see image below), and power it up.
To power the Pi, you plug a micro USB end of a USB cable into the Pi, and the other end into either a powered USB hub, a wall socket adapter (such as ones that most smartphones like the iPhone use), or the USB port of a computer. Whichever power source you choose, make sure there's enough current available to reliably power your Pi, especially if you plan to plug in a keyboard and mouse into the Pi's USB ports.
For our purposes, a keyboard and mouse are not required. You'll simply need to plug your Pi into your home network via the ethernet cable, although USB wireless adapters are available as well. From there we'll be working from the command-line.
Logging Into Your Pi
Once your Pi is up and running, powered via the micro USB port, and plugged into your home network, you can login via SSH from your desktop or laptop computer, or even from another Raspberry Pi. I do this sometimes to easily transfer files (via FTP or SCP) from one Pi to another. The default user ID is "pi", and default password is "raspberry". Note: Since most people have the same default login information for their Pi, you should change this password right away if you plan to access your Pi remotely in any way.
Upgrading Your OS Image — Don't Skip This!
Unless it has been updated since I wrote this, the image you installed is only set to see about 256MB of RAM. If you have the latest revision Pi, you have 512MB RAM, and there's no reason to waste half of it. If you run the "top" command and see ~188MB total RAM, you'll need to update the OS to see all available RAM. To do so, follow these steps:
- Ensure you have superuser privileges.
Type: sudo -i
- Enter the following command, which takes about five minutes to complete.
Type: apt-get update
- The final command is just slightly different than the first, so be careful entering it. Also, it takes over an hour to complete, so you'll need to be patient.
Type: apt-get upgrade
When the commands finish, reboot your Pi and run the top command again. This time, the total RAM should be close to 512MB.
Install Java SE Embedded
Running Java on your Pi means that you can tap into all of the available libraries and open-source code, as well as knowledge and talent in the developer community worldwide. To begin, download Java SE Embedded from Oracle. For the Pi, you'll need Java SE 7 Embedded for ARMv6/7 Linux Softfloat, found here. Note that there are other downloads available for different ARM versions, PowerPC, and even a small footprint x86 version.
Copy (via FTP or SCP) this file over to your Pi, unzip it, and you're ready to go. I placed the resulting directly right in my home directory. The technical requirements to install this revision of Java SE Embedded are: Linux kernel 2.6.28 or higher, glibc 2.9 or higher, VFP, SoftFP ABI, at least 32MB RAM available for Java, and ~45MB of disk/flash space.
Oracle Java Embedded Suite
Don't stop with just the JVM. You can install the Oracle Java Embedded Suite (JES), which includes an embedded version of Glassfish, Java DB, the Jersey web services framework, in addition to Java SE Embedded. So technically you can skip the previous step of installing standalone Java if you download the install JES, available here.
JES includes a scaled-back version of the Glassfish Java EE application server, but it does run most Java Servlets unchanged, and with the web services frameworks, it makes for an excellent web server / application interface. For the specifics, read the documentation.
Other Application Servers
Java ME v3.3
If you're using the Pi as a platform to develop and test a Java application for a much more constrained device (i.e., small memory or disk footprint), you can install Java ME on your Pi. This includes both the CDC and CLDC (for very small devices), with which you can test your embedded Java application. Specifically, the CLDC-based runtime is targeted for use on highly memory-constrained devices (up to 1MB), also known as Oracle Java ME Embedded. The CDC-based runtime is targeted for use on devices with slightly higher memory capacity (up to 10MB), also known as Oracle Java Micro Edition Embedded Client.
My Cool Project: Raspberry Pi --> Arduino via Java
If you're interested in my ongoing project, where I use Java on a Raspberry Pi to control an Arduino, which in turn will control some other electrical device via a PowerSwitch Tail, check out this video I recently posted to YouTube. I'll be updating YouTube with new videos as my project progresses. Be sure to share stories and videos of your own cool projects once you get Java up and running on your Raspberry Pi!