Al Williams

Dr. Dobb's Bloggers

Write Once, Debug on Arduino

December 06, 2013

Can you run Java programs on the Arduino? Maybe.

I seem to have a love/hate relationship with a few things that most people are crazy about. Two things that immediately come to mind: Arduino and Java. I've written a lot about why I am not crazy about Arduino (but, also, why I still sometimes use them). And I've written at least three books that had significant Java content, besides being the Java columnist for Web Techniques and the Java newsletter editor for Dr. Dobb's many years back. So don't make the mistake of thinking I just don't like things I don't know about. I know something about both of these tools and while I like some things about them, I also know what things I don't like.

Eric Bruno and I were talking around the (virtual) water cooler the other day about Arduino, and I mentioned to him that there are at least two projects I'm aware of that purport to let you run Java on the Arduino. If you double up on things I love/hate, it is probably like a genetics experiment: I'll either get all the love, all the hate, or another love/hate combination. You could argue that Java on such a small platform isn't very useful and I'd be hard pressed to rebut you. However, many people like programming in Java, and there is some novelty to being able to handle it on such a small platform.

The two projects in question are HaikuVM and uJ. The uJ system looked like it took a little work on your part to decide where to store bytecodes so I decided to look at HaikuVM instead. To be fair, neither of these are just for the Arduino, but both of them say they can support it.

The basic idea behind HaikuVM is pretty simple. Haiku uses javac to generate class files and then post processes them a bit. The resulting class files turn into data stuffed in C structures that you compile and link with the Haiku JVM and the system libraries to get an executable program (in the case of the Arduino, a hex file). That hex file gets loaded like any other and you have your Java program running on an Arduino.

Of course, HaikuVM provides access to low-level resources in addition to the normal Java constructs (for example, there is a file called haiku.avr.AVRConstants.java). Here is the obligatory blink example that is included with the package:

package arduino.tutorial;
import static haiku.avr.lib.arduino.WProgram.*;
public class BlinkWithThread extends Thread {
static byte ledPin = 13; // LED connected to digital pin 13
public void run() // run over and over again
{
while (true) {
digitalWrite(ledPin, HIGH); // sets the LED on
delay(1000); // waits for a second
digitalWrite(ledPin, LOW); // sets the LED off
delay(1000); // waits for a second
}
}
public static void main(String[] args) {
pinMode(ledPin, OUTPUT); // sets the digital pin as output
new BlinkWithThread().start();
}
}

Yes, that example uses a pretty ordinary Java thread. Further examples show how to do the same task with an interrupt or even programmed delays. You can also do JNI calls if you really need to access the bare metal.

As you might expect, there is a lot going on under the hood. The HaikuVM bootstraps a Java "microkernel" that provides the runtime environment and loads your program as well. You have to have a working C compiler for the AVR (in the case of the Arduino) and all the other pieces that contribute. Naturally, it all seems set up for Windows.

I'm not a big fan of Windows and I normally run Linux. However, the documentation gives you a few steps to get it all running on Linux. This started a series of frustrating events ranging from the gcc-avr package using hard links (incompatible with my multi-disk setup) to problems with avrdude working with the Leonardo (the one Arduino I am a fan of, but it has that funny USB serial port that disappears when you reset it). None of this is specifically a problem with HaikuVM and I really wanted to try it, so I broke out the Windows instance I run in VirtualBox. Unfortunately, the disappearing serial port bit me on VirtualBox too, so I pulled out a dual boot laptop and fired up an old copy of Windows Vista.

However, it hasn't been my week, and a recent network change resulted in the dreaded "Local Access" bug on Windows. I finally went back to a different Linux box and managed to get it working there — or, at least, apparently.

In the end, although I could successfully compile the examples into hex files, they did not seem to work on the Leonardo. The Leonardo is supported, although I had to tweak the avrdude line to make it work. Every example program I loaded did nothing (but did replace any program I had previously loaded using the Arduino IDE). There is another option used to produce a file the Arduino IDE can upload, but I never got that to work (some problem with the class files).

Ultimately, I gave up. The problem could have been with my javac compiler or with the version of gcc I was using. It would be a major science project to run it down.

In the end, I'm not sure what real value there is to running Java on very small platforms where you probably don't have networking (I really like writing network software in Java). However, if Java is your preferred tool — and you have the patience to make HaikuVM work — it might be worth checking out. Perhaps you'll have better luck than I did. If you make HaikuVM or uJ work, leave a comment with your experiences and perhaps it will fuel more water cooler chatter.

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.

Video

This month's Dr. Dobb's Journal

This month,
Dr. Dobb's Journal is devoted to mobile programming. We introduce you to Apple's new Swift programming language, discuss the perils of being the third-most-popular mobile platform, revisit SQLite on Android
, and much more!