Tuesday, July 03, 2012

Java on the Raspberry Pi

As anybody familiar with my blog will no doubt be aware, I'm a Java programmer by day, and one of my ideas for using my Pi is as a little web-server. I fancy having a go at writing one for myself, just to see what the fuss is all about ;-) So, Java is the logical choice for me! (At which point, any number of language nerdsgurus will insist that I'm made, and that I should do it in C/C++/C11/Fortran/Python/Bash ... you name it, anything but 'slow' Java)

Anyhow. Space is at a premium on the Pi, so I thought I'd try installing a JVM/JDK on my CIFS mounted file-system that actually lives on my desktop PC. Unfortunately, that filesystem is NTFS (underneath the hood), and that means it doesn't support Unix-style soft links... and the JDK that is available for the Pi (OpenJDK) is full of them, even once you get through the layers of soft links around the file system that lead to /usr/lib/jvm/java-6-openjdk. So, the network mount was out of the question. (Maybe someone, somewhere, has instructions for creating a virtual file system in a file on a network share, where the filesystem inside is something more native to Debian Squeeze Linux).

The not so bad news is that OpenJDK 6 is actually only (only?) 108Mbytes, which I can afford as long as I keep my project code on my projects drive. So, assuming your system is setup as I've described in previous posts:

  1. SSH/VNC to the Pi, logging in as 'pi'
  2. Run "ln -s /mnt/projects" to create a projects folder in my home directory that's actually safely squirrelled away on my PC. (auto-magically double backed-up every night via SyncToy and DropBox, by the way)
  3. Now install the OpenJDK version 6 (I'll save JDK 7 for another time as that's a whole different kettle of rather narsty fish!): run "sudo apt-get install openjdk-6-jdk"
  4. It takes a while, but when it's done, "which java" should give you:
    pi@raspberrypi:/$ which java
  5. and "java -version" should give:
    pi@raspberrypi:/$ java -version
    java version "1.6.0_18"
    OpenJDK Runtime Environment (IcedTea6 1.8.13) (6b18-1.8.13-0+squeeze1)
    OpenJDK Zero VM (build 14.0-b16, mixed mode)
  6. Now run "sudo apt-get clean", to remove the cached installation files from the various tools you've installed - I found this saved about 50Mbytes of space, so a not inconsiderable saving on such a small file system.
  7. Now to write some Java code.


  1. That is exactly my plan for my raspberry pi too!, how did that go? Were you able to run standalone java apps? what about a tomcat instance!?.

    Best regards.

    1. Yes it did work - including writing a small web server of my own.

      Tomcat might work, but I've not tried it.

      Jetty would also be worth a try if your looking at small web servers.

  2. Hi!

    I received today my raspi and I just downloaded and installed the openjdk from the repositories. I searched about installing Java on the raspi and I found this: http://www.oracle.com/technetwork/articles/java/raspberrypi-1704896.html
    java SE for Embedded. I'm wondering: why using this instead of using directly the openJDK for debian? Because for me works perfectly (it's true that the compilation takes a bit). It's about performance? The openJDK performance in the raspi is worst than the Embedded version?
    Do you know why?

    Best Regards

    1. Hi Shuco,
      looks like that article was published in August, some time after I posted mine in July, so I don't know if there are any performance reasons for their choice of the Oracle jdk. However, as it's an article on the Oracle site, I suspect that's their reason for using it. :)

      I've just reformatted my Pi as an XBMC machine, so I'm not in a position to compare them. But, if you do any comparative tests yourself, I'd be interested in the results.

  3. Thanks you, I installed Java, below is output

    pi@webserver ~ $sudo apt-get update
    pi@webserver ~ $sudo apt-get install openjdk-6-jdk

    pi@webserver ~ $ which java

    pi@webserver /usr/local $ java -version
    java version "1.6.0_24"
    OpenJDK Runtime Environment (IcedTea6 1.11.5) (6b24-1.11.5-1+rpi1)
    OpenJDK Zero VM (build 20.0-b12, mixed mode)


  4. I believe Openjdk does not have jit compiler so is slower. Tomcat7 runs but I find jsp files take about 10-15 seconds to open.