The Raspberry Pi is an inexpensive little ARM-based embedded computer developed as a teaching and experiment platform. I wanted to experiment running Java applications on my Raspberry Pi so I need to install a Java Runtime to execute Java-based applications.
I first installed the Open JDK on the device since this is the JRE flavor most commonly found in mainstream Linux OS distributions. Installing the Open JDK is quite simple. All you have to do is enter the following command and it will download and install the Open JDK.
apt-get install openjdk-6-jdk
However, the performance I experienced while using the Open JDK on the Raspberry Pi was quite disappointing and dare I say unusable for any reasonably sophisticated Java program. ChrisG posts some pretty revealing benchmarks using various JVMs in this blog article: http://www.raspberrypi.org/phpBB3/viewtopic.php?f=34&t=6369 I ran the same benchmark tests on my Pi and got very similar results. If you notice the Oracle JRE benchmark results in ChrisG's post you will see that they are an order of magnitude better in performance than the Open JDK Zero and Open JDK Cacao JVMs. For my goals, this make the Oracle JVM the only suitable candidate for running my Java applications.
This article will provide the necessary instructions on how to install the Oracle Java Runtime Environment on your Raspberry Pi.
All:
Windows:
Mac-OSX:
You can download the Oracle Java SE Embedded 7 JRE for ARMv6/7 Linux - Headless on this page:
http://www.oracle.com/technetwork/java/embedded/downloads/javase/index.html
Please note that you do unfortunately have to fill out a survey to download the JVM. This means that you cannot simply use a tool such as wget or curl to download the JVM directly to the Raspberry Pi. (oracle-fail!) Well I guess you could technically run "startx" and use a graphical browser in the X-Windows GUI to complete this step, but this article will use SCP to get the JVM file transferred to the Raspberry Pi.
After downloading the Oracle JRE to you desktop computer, we need to transfer it over to the Raspberry Pi. We will use SCP to transfer the file over the network. If you are running on a Windows desktop, then download and install WinSCP.
If you are using Mac OSX, you can download and install Cyberduck. The screens will look different but the goals are the same.
Create a new session in WinSCP using the IP address of you Raspberry Pi. The default authentication credentials for the Debian Squeeze image is username "pi" and password "raspberry". Save the session and then login. You may be prompted to accept the SSH fingerprint, choose "Yes" to accept and continue.
After successfully establishing a connection, select the drive and folder location in the left pane where you download the Oracle JRE file to on your local desktop system. In the right pane is the file system on the Raspberry Pi, we will leave it in it's default location in the "pi" user's home directory. Drag and drop the Oracle JRE file from the left pane to the right pane and WinSCP will start the file transfer process. You will be prompted with a transfer dialog, just click the "Copy" button to start the transfer.
When the file transfer is complete, you can close WinSCP (or CyberDuck).
The remaining steps should be performed directly on the console of the Raspberry Pi or using a SSH terminal connection with shell access. In the last step, we transfered the Oracle JRE file to the "pi" user's home directory. We should be logged in as the "pi" user and already in the user's home directory.
Lets create a new directory where we will install the JRE files to.
sudo mkdir -p -v /opt/java
Next, lets unpack the Oracle JRE tar file using this command
tar xvzf ~/ejre-7u4-fcs-b20-linux-arm-vfp-client_headless-12_apr_2012.gz
The unpacking process will take a few seconds to complete. It unpacks all the contents of the Oracle JRE tar file to a new directory named "ejre1.7.0_04" located in the user's home directory.
With the unpack complete its now time to move the new unpacked directory to the Java install location that we created earlier under "opt/java".
sudo mv -v ~/ejre1.7.0_04 /opt/java
We can also delete the original taf file as it is no longer needed
rm ~/ejre-7u4-fcs-b20-linux-arm-vfp-client_headless-12_apr_2012.gz
To complete the JRE installation we need to let the system know there is a new JVM installed and where it is located. Use the following command to perform this task.
sudo update-alternatives --install "/usr/bin/java" "java" "/opt/java/ejre1.7.0_04/bin/java" 1
And finally we also need to tell the system that we want this JRE to be the default JRE for the system. The following command will perform this action.
sudo update-alternatives --set java /opt/java/ejre1.7.0_04/bin/java
Now java is installed. To test and verify we can execute the java command using the version argument.
java -version
You should get the following response:
That's it the Oracle JRE is installed and ready for use.
Some Java programs require a JAVA_HOME environment variable to be configured on the system. Add the following line to you "/etc/environment" or your "~/.bashrc" file using your favorite text editor.
JAVA_HOME="/opt/java/ejre1.7.0_04"
Reboot or re-login to apply the export to your environment.
Coffee & Pie - what could be better!
Hinkmond Wong and Gary Collins of Oracle are going to introduce you to developing for the Raspberry Pi using Java Embedded technology.
In addition to demoing the Raspberry Pi running Java Embedded, they will cover:
* Raspberry Pi is a trademark of the Raspberry Pi foundation.
* Oracle and Java are registered trademarks of Oracle.
Article updated to include installation instructions based on the Debian "Wheezy" (2012-08-08) image.
The Oracle Java Development Kit for ARM (JDK) is now available as an alternative to the SE Embedded JRE. Here is a new article with step-by-step instructions on how to install the Oracle JDK.