NonStop Server for Java (NSJ) Programmer's Guide (NSJ 2.1+)
Java Native Interface (JNI)
The Sun Microsystems Java Native Interface (JNI) standard defines both the C-language APIs that enable Java methods to call
C and C++ methods and the way that C and C++ methods can start and interact with a Java Virtual Machine (JVM). NonStop
Java supports JNI and the Invocation API with the following modifications:
You must relink the JVM whenever you use new C or C++ routines in your Java programs.●
Multithreaded native C or C++ routines must use the same Pthread library and wrappers that the JVM uses, but you can
write any multithreaded code in Java.
●
When calling a C or C++ routine, function returns and parameters of type float or double are converted from Compaq
format to IEEE floating-point format and back. This conversion might result in loss of precision because of differences
between Compaq format and IEEE floating-point format.
●
Compaq provides a compaq.liblist system property to check for statically bound native libraries at load time.●
When naming library files, observe the following rules:
Do not use names that begin with Tandem, tandem, or tdm.●
Do not use the same file name for more than one library file, even if the two library files are in different directories.●
The remainder of this subsection explains:
Calling C or C++ Methods From Java●
Calling Java Methods From C or C++●
Using the compaq.liblist System Property●
For more information about JNI, see the Sun Microsystems JNI document
(http://www.javasoft.com/products/jdk/1.2/docs/guide/jni/index.html).
Calling C or C++ Methods From Java
To call C or C++ methods from Java:
Compile the Java code.1.
Use javah to generate header files.2.
Compile the C or C++ code. C++ code must be compiled using the -Wversion2 compiler command-line option.3.
Create either a relinkable library (.lib) file or an archive library (.a) file.4.
Place the newly created library (.a or .lib) file in /usr/tandem/java/lib.5.
Relink the Java Virtual Machine (JVM) by using the /usr/tandem/install/Makefile. For more information about
the Makefile, see the /usr/tandem/install/README file.
6.
The javahjni demo shows an example of how to create an archive file and link it in with a java executable.
The remainder of this subsection:
Explains how to write thread-safe native methods●
Explains how to use sockets●
Gives a debugging tip●
Writing Thread-Safe Native Methods
A thread that is sleeping or executing a blocked routine is blocked. NonStop Java runs the highest-priority thread that is not
blocked. If more than one thread has the highest priority, all of those threads get some processor time. Lower-priority threads are
guaranteed to run only when higher-priority threads are blocked. Lower-priority threads might run when higher-priority threads
are not blocked, but this result is not guaranteed. The Java Run-Time can suspend any thread, regardless of its priority, to run
another thread at the same priority.
You are encouraged to write threaded code in Java rather than in native C or C++.
If you do write threaded native C or C++ methods, they must:
Use the same Pthread library and wrappers that the JVM uses; that is, the OSS Pthread library●