| Author |
JNI_CreateJavaVM returns error
|
Rich Pelt
Greenhorn
Joined: Sep 07, 2005
Posts: 1
|
|
I am attempting to write a DLL. The method JNI_CreateJavaVM returns Ok on the first iterations but fails on all subsequent attempts. This is using VC++ version 6 and JNI 1.5. Attempting to create the JVM in DLLMain results in a hung program. What am I doing wrong? JNIEnv *env; JavaVM *jvm; JavaVMInitArgs vm_args; jint res; jclass cls; jmethodID mid; jstring jstr; jstring jstr1; const char* cpath; int i = 0; int j = 0; int len = 0; jobjectArray args; jobjectArray arr; char *outbuf; char **arrbuf; JavaVMOption options[1]; vm_args.version = JNI_VERSION_1_2; vm_args.nOptions = 1; options[0].optionString = "-Djava.class.path=C:\\POC\\SamDBRMI"; vm_args.options = options; vm_args.ignoreUnrecognized = JNI_FALSE; JNI_GetDefaultJavaVMInitArgs(&vm_args); res = JNI_CreateJavaVM(&jvm,(void**)&env,&vm_args); if (res < 0) { fprintf(stderr, "Can't create Java VM, Err=%d\n", (int) res); return 0; } cls = env->FindClass("DBClient"); if (cls == 0) { fprintf(stderr, "Can't find DBClient class\n"); goto finished; } mid = env->GetStaticMethodID(cls,"main","([Ljava/lang/String [Ljava/lang/String;"); if (mid == 0) { fprintf(stderr, "Can't find DBClient.Method for fetching candidates\n"); goto finished; } // The Query sent to the Database was hard coded here jstr = env->NewStringUTF(sql_query); if (jstr == 0) { fprintf(stderr, "Out of memory\n"); goto finished; } args = env->NewObjectArray(1,cls, jstr); if (args == 0) { fprintf(stderr, "Out of memory\n"); goto finished; } cpath = env->GetStringUTFChars(jstr, 0); printf("%s",cpath); env->ReleaseStringUTFChars(jstr, cpath); // Fetching data from Java Client & printing back in C++ arr = (jobjectArray)env->CallStaticObjectMethod(cls,mid,args); if (arr == 0 || arr == NULL) { fprintf(stderr, "Problem Here in arr fetching\n"); goto finished; } jstr = env->NewStringUTF("From the C++ Application \n"); if (jstr == 0) { fprintf(stderr, "Out of memory\n"); goto finished; } cpath = env->GetStringUTFChars(jstr, 0); printf("%s",cpath); env->ReleaseStringUTFChars(jstr, cpath); j = env->GetArrayLength(arr); if(j==0) { printf("%s","No Elements in the Array"); goto finished; } i = 0; if(j) { arrbuf = (char **) new char *[j + 1]; for(i = 0; i < j; i++) { jstr1 = (jstring)env->GetObjectArrayElement(arr,i); len = env->GetStringLength(jstr1); outbuf = (char *) new char[len + 1];env->GetStringUTFRegion(jstr1, 0, len, outbuf); arrbuf[i] = outbuf; env->DeleteLocalRef(jstr1); } } arrbuf[i] = NULL; *result = arrbuf; finished : jvm->DestroyJavaVM(); return j; }
|
 |
Vitaly Shelest
Greenhorn
Joined: Sep 28, 2005
Posts: 4
|
|
Try the tool at http://simtel.net/product.php[id]90910[SiteID]simtel.net
|
 |
 |
|
|
subject: JNI_CreateJavaVM returns error
|
|
|