HP-UX Programmer's Guide for Java 2

Table Of Contents
//create a new Java Virtual Machine
//and locate the class TestNonJavaCallingJava2
//and invoke the static method printInt
#include <jni.h>
#include <iostream.h>
#include <stdlib.h>
int main()
{
JNIEnv *env;
JavaVM *jvm;
JNIEnv jni;
JavaVM vmi;
JavaVMInitArgs vm_args;
JavaVMOption options[4];
jint res;
cout << "beginning execution..." << endl;
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 and initialize a java vm, return a JNI interface ptr in env
cout << "before CreateJavaVM" << endl;
res = JNI_CreateJavaVM(&jvm,(void **)&env,&vm_args);
if (res != 0) {
cerr << "JNI_CreateJavaVM failed %d\n" << res << "." << endl;
exit(1);
}
else cout << "after CreateJavaVM " << endl;
// Find the class
cout << "before FindClass" << endl;
jclass cls=env->FindClass("TestNonJavaCallingJava2");
if (cls == 0) {
cerr << "Could not locate class NonJavaCallingJava2 in the class path" \
<< options[0].optionString << "." << endl; exit(1);
}
else cout << "after FindClass" << endl;
// Find the method
jmethodID mid=env->GetStaticMethodID(cls, "printInt", "(I)V");
if (mid == 0) {
cerr << "Could not locate method printInt with signature (I)V"
" in the class TestNonJavaCallingJava." << endl;
exit(1);
}
// Invoke the method
env->CallStaticVoidMethod(cls, mid, 100);
// we are done
jvm->DestroyJavaVM();
}
$ aCC -c -ext -mt -I<java_dir>/include -I<java_dir>/include/hp-ux \
aCC_main.C
56 Using Java™ 2 JNI on HP-UX