Using Java on Dijkstra

Introduction

This page summarizes the information you need for building Java applications on Dijkstra. It may or may not remain relevant after the Spring 2003 semester.

Installed Software

Version 1.4.1 of Sun's Software Development Kit for Java is installed in /usr/local/j2sdk1.4.1_01. To avoid having to change setup parameters every time I install a new version, there is a symbolic link from that directory to /usr/local/java.

The SDK commands related to Java (javac, java, javah, javadoc, etc.) are in /usr/local/java/bin, so that directory must be in your PATH in order to use them.

IBM's speedy Jikes compiler (jikes) is in /usr/local/bin, a common place to put locally-installed software (as opposed to the programs that are installed with the SuSE Linux distribution), so you need to be sure that directory is in your PATH too.

The jikes compiler uses the environment variable JIKESPATH to locate the standard class libraries that are used by Java programs. This variable has to be set to /usr/local/java/jre/lib/rt.jar for the jikes compiler to work. (In class I erroneously named this variable "JIKES_HOME" instead of "JIKESPATH.")

A nice feature of the jikes compiler comes when you invoke it with the "++" command line option. For example, the command, jikes ++ *.java will compile all .java files in the current directory, but the compiler will not exit. Instead, it will prompt you to press Enter when you want to compile your .java files again. Like make, jikes is smart and will recompile only those .java files that have changed since the last compilation. If you log into Dijkstra from two different windows, you can set the jikes compiler running in one window while you edit and test your code in another one.

To use JNI, you need to #include the jni.h header file in your .cc program. Furthermore, jni.h #includes jni_md.h, so you need to tell the g++ compiler driver what directories to search for these two header files using the "-I" command line option. You can do this using make by setting CXXFLAGS as follows:

CXXFLAGS = -g -Wall =Wwrite-strings -I/usr/local/java/include \
-I/usr/local/java/linux

Libraries

To use JNI, your C++ functions have to be in a shared library. In order for the Java VM to load a library ( System.loadLibrary() ), the directory containing the library has to be part of the LD_LIBRARY_PATH, which, like PATH, is a colon-separated list of directories. If the library file is in the current directory, you can just put . in the value of this variable. For example, here's a rule from a Makefile that could be used to run the "HelloWorld" sample program from the JNI tutorial at Sun:

run : libhello.so HelloWorld.class
<tab>LD_LIBRARY_PATH=.:$(LD_LIBRARY_PATH) java HelloWorld

This rule uses a feature of the shell that lets you set the value of an environment variable just for the execution of a single command; once the command completes, the variable returns to its original value. There is nothing that says the rule would have to be written this way, but the example gives you a chance to see another way to use environment variables.