08/11/2010

Real-Time Java HelloWorld

Are you starting with Real-time Java and don't know where to look first? Here is a step-by-step tutorial showing you how to install and run simple RT Java programs.

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.
1.1  Install the RTJ VM
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
You should see:
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.
You can safely ignore this one for testing RTSJ.

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
This happened to me both on Ubuntu and Fedora OS. There is a detailed description of this problem here (as it appears, I was the first person to identify this issue).

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:
Further blog-post on the same topic:
  • http://viswanathj.wordpress.com/2011/04/09/installing-java-rts-on-ubuntu-2/

7 comments:

  1. This was a great post. It solved the problems I kept running into. Saved my life. Thank you!

    ReplyDelete
  2. I ran into the problem

    " failure on line 824Error: failed /usr/lib/jrts-2.2_fcs/jre/lib/amd64/server/libjvm.so, because libcap.so.1: cannot open shared object file: No such file or directory "

    on Ubuntu 10.04 LTS with rt-linux

    your blog entry helped me fix the problem.

    Cheers

    ReplyDelete
    Replies
    1. As you mentioned you are running ubuntu 10.04 with rt-kernel. Can you help me in patching rt-kernel on ubuntu 13.10.

      Delete
  3. you saved my life :D

    Thank you very very much

    ReplyDelete
  4. I cannot download java_rts-2_2-fcs-bin-b19-linux-i586-eval_academic.zip
    could any one put link to it in 4shared or any sharing site

    ReplyDelete
    Replies
    1. I cannot download too.. does anyone know the status, and when it would be available?

      Delete
    2. There some copies of the file on some servers... search for the exact filename =)
      BUT ... it's Java 1.5 Update 20... lots of things will npt run (You can run Eclipse with OpenJDK and define project specifiv the RTS as JRE))

      Better try IBM WebSphere RealTime or the JamaicaVM!

      Delete