1. Install Real-time Java VM
There is a bunch of RT VMs that you can donwload and use but perhaps the one that is the easiest to install and to play with is Oracle's Java RTS. The current version is Java RTS 2.2. After filling the download form, you should be able to download it to your desktop. The name of the donwloaded file will be something like : java_rts-2_2-fcs-bin-b19-linux-i586-eval_academic.zip
1.0 Requirements
- RT Linux - if you want to really run with real-time priorities. However, for starting and learning how to work with RT VMs, a regular linux should do - try eg. Ubuntu or Fedora OS.
untar downloaded distributable into /opt/jrts2.2 directory. Let's call it RTJAVA_HOME.
$ export RTJAVA_HOME=/opt/jrts2.2/
For more details, see SUN Java RTS Technical Documentation. Java RTS is distributed under restricted academic license that expires after 365 days but don't worry, you can just dowload a new distribution and re-install it.
1.2. Verify the Installation
Type:
$ $RTJAVA_HOME/bin/java -version
java version "1.5.0_20"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_20_Java-RTS-2.2_fcs-b19_RTSJ-1.0.2)
Java Real-Time System HotSpot(TM) 64-Bit Client VM (build 1.5.0_20-b19, mixed mode)
In case you are not running in a real-time environment or your linux does not have the rt-kernel extensions, you will also see the following warning:
Java Real-Time System HotSpot(TM) 64-Bit Client VM warning: Unable to lock pages into memory: insufficient privileges
Java Real-Time System HotSpot(TM) 64-Bit Client VM warning: Cannot use the real-time scheduling class.
1.3. A missing libcap.so problem
However, a problem can occur when running the "./bin/java" on some systems:
$RTJAVA_HOME/bin/java -version
dl failure on line 824Error: failed /opt/sunrts-2.2/jre/lib/i386/server/libjvm.so, because libcap.so.1: cannot open shared object file: No such file or directory
Solution to the missing libcap.so.1 lib is to locate a libcap.so.2 and rename it as libcap.so.1
$ locate libcap.so.2
$ sudo cp /lib/libcap.so.2 /lib/libcap.so.1
2. Runnig a Real-time HelloWorld program
Now we can finally run some real-time Java code. A classical real-time Java hello world example is below:
import javax.realtime.RealtimeThread; public class HelloRealtimeWorld extends RealtimeThread { String message; public void run(){ message = "Hello Realtime World"; } public static void main(String args[]){ HelloRealtimeWorld thread = new HelloRealtimeWorld(); thread.start(); try{ thread.join(); } catch(InterruptedException ie){ System.err.println("got interrupted"); return; } System.out.println(thread.message); } }
To compile the Real-Time HelloWorld class:
$ $RTJAVA_HOME/bin/javac -classpath $RTJAVA_HOME/jre/lib/rt2.jar
-d build/ HelloRealtimeWorld.java
And, to run:
$ $RTJAVA_HOME/bin/java -cp build/ HelloRealtimeWorld
Hello Realtime World!
And we are done!
Further resources on RT Java:
- Concurrent and Real-time Java Programming book,
- try our CDx - Real-Time Java Benchmark,
- Oracle's RTSJ Introduction
- Embedded and Real-time Computing class taught by Prof. Vitek (check out the slides for selected topics)
Further blog-post on the same topic:
- http://viswanathj.wordpress.com/2011/04/09/installing-java-rts-on-ubuntu-2/