Channels ▼

Eric Bruno

Dr. Dobb's Bloggers

Java Apps on the Raspberry Pi

May 21, 2013

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:

  1. Ensure you have superuser privileges.
    Type: sudo -i

  2. Enter the following command, which takes about five minutes to complete.
    Type: apt-get update

  3. 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

IBM offers the Websphere Liberty Profile download, specifically for the Raspberry Pi, available here. For more information on how to install and use it, along with videos showing it in use, go here.

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.

You can find versions of Java ME built specifically for the Raspberry Pi here. To see Java ME on the Pi in action, check out this video.

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!

Happy coding!
-EJB

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Comments:

ubm_techweb_disqus_sso_-06a6faddc430162ab6c827d900667643
2013-08-19T01:29:45

Thanks, I haven't heard of it. I'll take a look now.


Permalink
ubm_techweb_disqus_sso_-120dc5ad857f47c01994b51e360887d5
2013-08-10T12:40:55

Eric, have you heard of pcDuino? It has arduino native built in Linux.


Permalink


Video