This example follows the standard Invocation API paradigm.
It does the following:
When you create the program, the QJVAJNI or QJVAJNI64 service program provides the JNI_CreateJavaVM Invocation API function. JNI_CreateJavaVM creates the Java virtual machine.
These service programs reside in the system binding directory and you do not need to explicitly identify them on a control language (CL) create command. For example, you would not explicitly identify the previously mentioned service programs when using the Create Program (CRTPGM) command or the Create Service Program (CRTSRVPGM) command.
One way to run this program is to use the following control language command:
SBMJOB CMD(CALL PGM(YOURLIB/PGMNAME)) ALWMLTTHD(*YES)
Any job that creates a Java virtual machine must be multithread-capable. The output from the main program, as well as any output from the program, ends up in QPRINT spooled files. These spooled files are visible when you use the Work with Submitted Jobs (WRKSBMJOB) control language (CL) command and view the job that you started by using the Submit Job (SBMJOB) CL command.
#define OS400_JVM_12 #include <stdlib.h> #include <stdio.h> #include <fcntl.h> #include <string.h> #include <jni.h> /* Specify the pragma that causes all literal strings in the * source code to be stored in ASCII (which, for the strings * used, is equivalent to UTF-8) */ #pragma convert(819) /* Procedure: Oops * * Description: Helper routine that is called when a JNI function * returns a zero value, indicating a serious error. * This routine reports the exception to stderr and * ends the JVM abruptly with a call to FatalError. * * Parameters: env -- JNIEnv* to use for JNI calls * msg -- char* pointing to error description in UTF-8 * * Note: Control does not return after the call to FatalError * and it does not return from this procedure. */ void Oops(JNIEnv* env, char *msg) { if ((*env)->ExceptionOccurred(env)) { (*env)->ExceptionDescribe(env); } (*env)->FatalError(env, msg); } /* This is the program's "main" routine. */ int main (int argc, char *argv[]) { JavaVMInitArgs initArgs; /* Virtual Machine (VM) initialization structure, passed by * reference to JNI_CreateJavaVM(). See jni.h for details */ JavaVM* myJVM; /* JavaVM pointer set by call to JNI_CreateJavaVM */ JNIEnv* myEnv; /* JNIEnv pointer set by call to JNI_CreateJavaVM */ char* myClasspath; /* Changeable classpath 'string' */ jclass myClass; /* The class to call, 'NativeHello'. */ jmethodID mainID; /* The method ID of its 'main' routine. */ jclass stringClass; /* Needed to create the String[] arg for main */ jobjectArray args; /* The String[] itself */ JavaVMOption options[1]; /* Options array -- use options to set classpath */ int fd0, fd1, fd2; /* file descriptors for IO */ /* Open the file descriptors so that IO works. */ fd0 = open("/dev/null1", O_CREAT|O_TRUNC|O_RDWR, S_IRUSR|S_IROTH); fd1 = open("/dev/null2", O_CREAT|O_TRUNC|O_WRONLY, S_IWUSR|S_IWOTH); fd2 = open("/dev/null3", O_CREAT|O_TRUNC|O_WRONLY, S_IWUSR|S_IWOTH); /* Set the version field of the initialization arguments for J2SDK v1.3. */ initArgs.version = 0x00010002; /* To use J2SDK v1.4, set initArgs.version = 0x00010004; */ /* To use J2SDK v1.5, set initArgs.version = 0x00010005; */ /* Now, you want to specify the directory for the class to run in the classpath. * with Java2, classpath is passed in as an option. * Note: You must specify the directory name in UTF-8 format. So, you wrap * blocks of code in #pragma convert statements. */ options[0].optionString="-Djava.class.path=/CrtJvmExample"; /*To use J2SDK v1.4 or v1.5, replace the '1.3' with '1.4' or '1.5'. options[1].optionString="-Djava.version=1.3" */ initArgs.options=options; /* Pass in the classpath that has been set up. */ initArgs.nOptions = 2; /* Pass in classpath and version options */ /* Create the JVM -- a nonzero return code indicates there was * an error. Drop back into EBCDIC and write a message to stderr * before exiting the program. */ if (JNI_CreateJavaVM("myJVM, (void **)"myEnv, (void *)"initArgs)) { #pragma convert(0) fprintf(stderr, "Failed to create the JVM\n"); #pragma convert(819) exit(1); } /* Use the newly created JVM to find the example class, * called 'NativeHello'. */ myClass = (*myEnv)->FindClass(myEnv, "NativeHello"); if (! myClass) { Oops(myEnv, "Failed to find class 'NativeHello'"); } /* Now, get the method identifier for the 'main' entry point * of the class. * Note: The signature of 'main' is always the same for any * class called by the following java command: * "main" , "([Ljava/lang/String;)V" */ mainID = (*myEnv)->GetStaticMethodID(myEnv,myClass,"main", "([Ljava/lang/String;)V"); if (! mainID) { Oops(myEnv, "Failed to find jmethodID of 'main'"); } /* Get the jclass for String to create the array * of String to pass to 'main'. */ stringClass = (*myEnv)->FindClass(myEnv, "java/lang/String"); if (! stringClass) { Oops(myEnv, "Failed to find java/lang/String"); } /* Now, you need to create an empty array of strings, * since main requires such an array as a parameter. */ args = (*myEnv)->NewObjectArray(myEnv,0,stringClass,0); if (! args) { Oops(myEnv, "Failed to create args array"); } /* Now, you have the methodID of main and the class, so you can * call the main method. */ (*myEnv)->CallStaticVoidMethod(myEnv,myClass,mainID,args); /* Check for errors. */ if ((*myEnv)->ExceptionOccurred(myEnv)) { (*myEnv)->ExceptionDescribe(myEnv); } /* Finally, destroy the JavaVM that you created. */ (*myJVM)->DestroyJavaVM(myJVM); /* All done. */ return 0; }
For more information, see Java Invocation API.