HP-UX Programmer's Guide for Java 2

Table Of Contents
public native static void initialize();
public native static void sayHelloWorld();
public static void main(String args[]) {
String libname = args[0];
try {
System.loadLibrary(libname);
System.out.println("Library " + libname + " successfully loaded");
} catch (UnsatisfiedLinkError err) {
System.out.println("error: " + err);
return;
}
System.out.println("initialize C++ runtime");
initialize();
System.out.println("Calling sayHelloWorld");
sayHelloWorld();
System.out.println("All done!");
}
}
The code above is very similar to the code in the C-based example; the only differences
are the addition of the declaration and invocation of the native method initialize().
With the PA-RISC version of HP aC++ some internal C++ runtime data structures need
to be initialized before transferring control to any C++ code. The initialize()
method will perform the necessary initialization. With the Integrity version of HP aC++
this initialization step is no longer needed and the initialize() method can be
omitted.
Compile this class:
$ <java_dir>/bin/javac -verbose TestJava2CallingNative.java
Output:
TestJava2CallingNative.class
Generate the JNI header file for this class. You must have the current directory in your
CLASSPATH for the javah command to find your newly compiled class file.
$ <java_dir>/bin/javah -verbose -jni TestJava2CallingNative
Output:
TestJava2CallingNative.h
Here is the sample C++ native method implementation for initialize and sayHelloWorld:
//
// File aCCImpl.C
//
#include "TestJava2CallingNative.h"
#include <iostream.h>
extern "C" {
50 Using Java™ 2 JNI on HP-UX