*********************************************************************** * To Compile: * * 1. CRTRPGMOD MODULE(YourLib/JVMMODULE) SRCFILE(YourLib/QRPGLESRC) * Which is the same as option 15 on PDM * * 2. Create a service program or leave as a module. * 3. Optionally, place in a binding directory. * 4. Bind to your calling program either directly * or through your binding directory. *********************************************************************** H NoMain /Copy QSysInc/QRpgLeSrc,JNI /Copy KVandeverS/QRpgLeSrc,JVMModuleP //-------------------------------------------------- // destroyJVM //----------------------------------------------------- P destroyJVM B EXPORT D destroyJVM PI N D rc S LIKE(jint) D rpgRc S N D jvm S * DIM(1) D env S * D bufLen S LIKE(jsize) INZ(%elem(jvm)) D nVMs S LIKE(jsize) /free //---------------------------------------------------------------- // See if there’s a JVM started //---------------------------------------------------------------- rc = JNI_GetCreatedJavaVMs (jvm : bufLen : nVMs); //---------------------------------------------------------------- // If JVM is started, destroy it //---------------------------------------------------------------- if (rc = 0 and nVMs > 0); JavaVM_P = jvm(1); rc = DestroyJavaVM (jvm(1)); endif; if (rc = 0); return *ON; else; return *OFF; endif; /End-Free P destroyJVM E //---------------------------------------------------------------- // getJniEnv //---------------------------------------------------------------- P getJniEnv B EXPORT D getJniEnv PI * D rc S LIKE(jint) D rpgRc S N D jvm S * DIM(1) D env S * D bufLen S LIKE(jsize) INZ(%elem(jvm)) D nVMs S LIKE(jsize) D initArgs DS LIKEDS(JDK1_1InitArgs) D attachArgs DS LIKEDS(JDK1_1AttachArgs) /Free //---------------------------------------------------------------- // See if there’s a JVM started //---------------------------------------------------------------- rc = JNI_GetCreatedJavaVMs (jvm : bufLen : nVMs); //---------------------------------------------------------------- // If JVM is started, just attach to it, to get the env pointer //---------------------------------------------------------------- if (rc = 0 and nVMs > 0); attachArgs = *ALLX'00'; JavaVM_P = jvm(1); rc = AttachCurrentThread (jvm(1): env : %addr(attachArgs)); //---------------------------------------------------------------- // If JVM is not started, start it with the default classpath. //---------------------------------------------------------------- else; initArgs = *ALLX'00'; rc = JNI_GetDefaultJavaVMInitArgs (%addr(initArgs)); if (rc = 0); rc = JNI_CreateJavaVM (jvm(1) : env : %addr(initArgs)); endif; endif; if (rc = 0); return env; else; return *NULL; endif; /end-free P getJniEnv E