sobota, 19 stycznia 2013

Java and RPi

If you gave a java developer a Raspberry Pi board, what programming language would he choose? You can make additional assumption, that he would be interested in writing a web application for RPi.
He would choose:
  1. C++
  2. Python
  3. or maybe Java?

Please send your answer anywhere you wish, in the meantime we start installing Java on RPi.

Most searches in google, if you want to find phrase "java raspberry pi", ends with plenty of links to articles, how to install the only proper one version, namely oracle java. But I would like to install for starters an open version.

Actually, installation process is not very sophisticated:
sudo apt-get install openjdk-7-jdk

When it's done, you can verify it quickly:
myuser@raspberrypi ~ $ java -version
java version "1.7.0_07"
OpenJDK Runtime Environment (IcedTea7 2.3.2) (7u7-2.3.2a-1+rpi1)
OpenJDK Zero VM (build 22.0-b10, mixed mode)

When it comes to web application, web container could be quite handy when running application. So far I haven't used jetty, therefore:
sudo apt-get install jetty8

When installation is completed (ignore an error while starting jetty) you need to update you startup scripts. But first, the only valid text editor should be installeed:
sudo apt-get install vim

Before we update a configuration file, let's see where java has landed:
update-alternatives --list java

and we get:
/usr/lib/jvm/java-7-openjdk-armhf/jre/bin/java

so java home is: /usr/lib/jvm/java-7-openjdk-armhf

Knowing all that important stuff we can modify a script file:
sudo vim /etc/default/jetty8

first thing, we shall allow jetty to start along wiht the system:
# change to 0 to allow Jetty to start
NO_START=0

after that, setting JAVA_HOME is proper thing to do:
# Home of Java installation.
JAVA_HOME=/usr/lib/jvm/java-7-openjdk-armhf

If we completed our work now, jetty could be accessed only on... localhost. In order to access web server from anywhere, we need to set JETTY_HOST as well:
# Listen to connections from this network host
# Use 0.0.0.0 as host to accept all connections.
# Uncomment to restrict access to localhost
#JETTY_HOST=$(uname -n)
JETTY_HOST=0.0.0.0

Let's restart jetty:
sudo /etc/init.d/jetty8 restart

and what we get is default page for our jetty server:



However...
I would like to have the possibility to use oracle's jdk as well.
But...
It's not so easy for me... At least not on a wheezy version I have installed nor with jdk 1.7.0_10.
The problem is that I had wanted to have more efficient system, therefore I've installed wheezy with hardware ABI, and current oracle's jdk can use only slower soft-bloat ABI.

Fortunatelly, Oralce released "early access" jdk 1.8 with JavaFX included (you can get that package from http://jdk8.java.net/fxarmpreview/)

Whern you download it and unpack in /usr/lib/jvm directory, following command should be executed:
myuser@raspberrypi ~ $ sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.8.0/bin/java 1
myuser@raspberrypi ~ $ sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk1.8.0/bin/javac 1

One more thing to - setting which java alternative should be used:
myuser@raspberrypi ~ $ sudo update-alternatives --config java
There are 3 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-7-openjdk-armhf/jre/bin/java   1063      auto mode
  1            /usr/lib/jvm/java-7-openjdk-armhf/jre/bin/java   1063      manual mode
  2            /usr/lib/jvm/jdk1.8.0/bin/java                   1         manual mode

Press enter to keep the current choice[*], or type selection number: 2

myuser@raspberrypi ~ $ sudo update-alternatives --config javac
There are 3 choices for the alternative javac (providing /usr/bin/javac).

  Selection    Path                                         Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-7-openjdk-armhf/bin/javac   1063      auto mode
  1            /usr/lib/jvm/java-7-openjdk-armhf/bin/javac   1063      manual mode
  2            /usr/lib/jvm/jdk1.8.0/bin/javac               1         manual mode

Press enter to keep the current choice[*], or type selection number: 2

Version check is simple:
myuser@raspberrypi ~ $ java -version
java version "1.8.0-ea"
Java(TM) SE Runtime Environment (build 1.8.0-ea-b36e)
Java HotSpot(TM) Client VM (build 25.0-b04, mixed mode)

Last thing to is is changing jetty configuration. I did that, and for now everything seems to work fine.

Brak komentarzy:

Prześlij komentarz