Java on the Raspberry Pi

Java is now available for the Raspberry Pi through the Pi4J Project - more details here. As a Java developer myself, I decided to see how easy/difficult it would be to install Java on my Pi - so here's how I went about it. All the information below is available from Pi4J, but I thought it would be helpful to document the steps I went through.

 

Operating System

 

First off, you must use the Debian "Wheezy" operating system on your Pi - pi4J wont work with Raspbian (which is what I had installed when I started). The reason for this is apparently:

 

 

It turns out that this is an issue related to hard float versus soft float. The Pi4J native library was compiled against the Debian "Wheezy" soft-float image and users attempting to run it on the Raspbian "Wheezy" hard-float image are encountering this issue.

 

 

If you haven't got Debian Wheezy installed, you need to get that before you go any further - you can get it from http://www.raspberrypi.org/downloads. If you aren't to sure about installing there's a helpful page available at http://elinux.org/RPi_Easy_SD_Card_Setup.

 

JDK Install

 

Once you have got this installed you can start on the jdk installation. What I did was to download this to my PC and then copy it to my Pi using WinSCP (available from http://winscp.net/eng/index.php). Get the JDK from  http://www.oracle.com/technetwork/java/javase/downloads/index.html. You need to click on the JDK DOWNLOAD button (as shown below) and then find the jdk-7u10-linux-arm-sfp,tar.gz distribution, and download it to your Pc.

 

getjdk1.pnggetjdk2.png

 

Once the tar file in on your PC, copy it to the Pi (e.g. using winSCP). I put it in the pi user home directory (/home/pi) - and the following instructions assume it is there.

 

Open up a command prompt on your Pi, and then go through the following steps.

 

  • Untar the jdk distribution (i.e. the tar file copied above)

 

 

tar xvzf ~/jdk-7u10-linux-arm-sfp.gz

 

  • Create a java directory and move the untarred distribution to it. delete the tar file once we are done (don't need it any more)

 

 

sudo mkdir -p -v /opt/java
sudo mv -v ~/jdk1.7.0_10 /opt/java/
rm ~/jdk-7u10-linux-arm-sfp.gz

 

Create paths to the java and javac commands (so that they can be used) anywhere. When you have done this try the java --version command - if everything is done correctly this will return some information about the java installation

 

sudo update-alternatives --install "/usr/bin/java" "java" "/opt/java/jdk1.7.0_10/bin/java" 1
sudo update-alternatives --set java /opt/java/jdk1.7.0_10/bin/java
sudo update-alternatives --install "/usr/bin/javac" "javac" "/opt/java/jdk1.7.0_10/bin/javac" 1
sudo update-alternatives --set javac /opt/java/jdk1.7.0_10/bin/javac

java -version


Java is now installed!!  there are a few further steps required to set up the environment variables, so do the following ;

Some Java programs require a JAVA_HOME environment variable to be configured on the system.  Add the following line to you "/etc/environment" using your favourite text editor.

JAVA_HOME="/opt/java/jdk1.7.0_06"


Also, edit your "~/.bashrc" file using this command

nano ~/.bashrc

and add the following two lines to the bottom of the file and save.

export JAVA_HOME="/opt/java/jdk1.7.0_06"
export PATH=$PATH:$JAVA_HOME/bin

 

Installing Pi4J

 

Pi4J is installed quite simply on the Pi by using the following commands which will download and install the distribution;

 

wget http://pi4j.googlecode.com/files/pi4j-0.0.5.deb
sudo dpkg -i pi4j-0.0.5.deb

 

Pi4J is now installed in /opt/pi4j. This comes with some example classes and a build script which you can now run. this will also test that your java install is set up correctly. Navigate to the examples root and run the build as follows

 

cd /opt/pi4j/examples/
./build

 

 

You can now test the examples. I have a PiFace connected to my Pi, and there are a couple of test classes on mine so I ran the following command to run one of them. Note the class declaration which you must use in the java command in order to pick up the P14J libraries. You also have to be in the correct location to pick up the class - i.e. /opt/pi4j/examples

 

cd /opt/pi4j/examples/
sudo java -classpath .:classes:/opt/pi4j/lib/'*' PiFaceGpioExample

 

And that's it - Java installed and classes running in a few simple steps!

 


There's more information available at the 'official' oracle link here.

 

Resources