Forum Stats

  • 3,837,409 Users
  • 2,262,256 Discussions
  • 7,900,271 Comments

Discussions

JNI fails to create a JavaVM randomly

860517
860517 Member Posts: 1
edited May 11, 2011 4:25PM in Java Native Interface (JNI)
Below is a small code snippet in which we try to invoke a small Java function in native code.

#include <stdio.h>
#include <jni.h>

JNIEnv* createJavaVM(JavaVM ** jvm)
{
JNIEnv *env;
JavaVMInitArgs vm_args;
JavaVMOption options;

options.optionString = "-Djava.class.path=C:\\Documents and Settings\\ankurp\\J_workspace\\AlertTest\\bin"; //Path to the java output dir...

vm_args.version = JNI_VERSION_1_6; //JDK version. This indicates version 1.6
vm_args.nOptions = 1;
vm_args.options = &options;
vm_args.ignoreUnrecognized = 0;

int ret = JNI_CreateJavaVM(jvm, (void**)&env, &vm_args);

if(ret < 0)
printf("\nUnable to Launch JVM\n");

return env;
}

int main(int argc, char* argv[])
{
JNIEnv *env = NULL;
JavaVM *jvm = NULL;

env = createJavaVM(&jvm);

if (env == NULL)
return 1;

jclass classAlertTest = env->FindClass("AlertTest");

jmethodID midMain = NULL, midAlertFunc = NULL;

if (classAlertTest != NULL) {
midMain = env->GetStaticMethodID(classAlertTest, "main", "([Ljava/lang/String;)V");
midAlertFunc = env->GetStaticMethodID(classAlertTest, "alertFunc","(Ljava/lang/String;Ljava/lang/String;)V");
}

if (midAlertFunc) {
jstring message = env->NewStringUTF("Welcome!!!");
jstring title = env->NewStringUTF("Hello...");

env->CallStaticIntMethod(classAlertTest, midAlertFunc, message, title);
}

jvm->DestroyJavaVM();

env = createJavaVM(&jvm);

if (env == NULL)
return 2;

return 0;
}

The code works fine for first invovation of createJavaVM. But for second call of JNI_CreateJavaVM -1 is returned.

Our actual workflow is a Fdk plugin (a dll) of Framemaker, in which some C++ code invokes some java code (similar to what above code is doing).
The problem occurs that JNI_CreateJavaVM fails for first call itself(with -1 return value) "randomly". Our guess is probably FrameMaker takes lot of memory.

Is there anyway to overcome this problem? Since this problem is random in nature, we are stuck at C++/C to Java integration.

Waiting for some solution which will complete our integration.

Thanks

Edited by: 857514 on May 11, 2011 3:22 AM

Answers

This discussion has been closed.