HP-UX Programmer's Guide for Java 2

Table Of Contents
NOTE: The examples below work as is for Java™ 1.3.1.x and 1.4.2.x on PA-RISC. If
you wish to take advantage of the new JNI features in 1.4.2, in the example below you
should change the line vm_args.version = JNI_VERSION_1_2; to
vm_args.version = JNI_VERSION_1_4;
Below is an example of how to dynamically load the shared library libjvm.sl,
(libjvm.so for Integrity), create a new JVM, locate the class
TestNonJavaCallingJava2, and invoke the static method printInt.
#include <jni.h>
#include <stdlib.h>
#include <dl.h>
typedef int (*JNI_CreateJavaVM_t)(JavaVM**, void**, void*);
int main()
{
JNIEnv *env;
JavaVM *jvm;
JNIEnv jni;
JavaVM vmi;
JavaVMInitArgs vm_args;
JavaVMOption options[4];
jint res;
printf ("beginning execution...\n");
options[0].optionString = (char *) "-Djava.class.path=."; /* user classes \
*/
vm_args.version = JNI_VERSION_1_2;
vm_args.options = options;
vm_args.nOptions = 1;
vm_args.ignoreUnrecognized = JNI_FALSE;
// load libjvm.sl (PA-RISC) or libjvm.so (Integrity)
shl_t libsym = shl_load("libjvm.sl", BIND_IMMEDIATE|DYNAMIC_PATH, 0L);
if (libsym == 0) {
fprintf(stderr, "Unable to shl_load libjvm.sl\n");
exit (1);
}
else
fprintf(stdout, "shl_loaded libjvm.sl successfully!\n");
// find function JNI_CreateJavaVM
JNI_CreateJavaVM_t func = NULL;
res = shl_findsym(&libsym, "JNI_CreateJavaVM", TYPE_PROCEDURE, &func);
if (res == 0)
fprintf(stdout, "Found symbol JNI_CreateJavaVM!\n");
else {
fprintf(stderr, "Unable to find symbol JNI_CreateJavaVM!\n");
exit (1);
}
// load and init a java vm, return a JNI interface ptr in env
printf ("before CreateJavaVM\n");
res = (*func)(&jvm, (void **)&env, (void *)&vm_args);
if (res != 0) {
fprintf (stderr, "JNI_CreateJavaVM failed. %d\n", res);
exit (1);
60 Using Java™ 2 JNI on HP-UX