aspose file tools*
The moose likes Other JSE/JEE APIs and the fly likes JNI_CreateJavaVM returns error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "JNI_CreateJavaVM returns error" Watch "JNI_CreateJavaVM returns error" New topic
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
 
jQuery in Action, 2nd edition
 
subject: JNI_CreateJavaVM returns error
 
Similar Threads
Invoking JVM through C++ code
JNI : undefined reference to JNI_CreateJavaVM()
Here is how to create process and Image APPName.exe file instead of java or javaw
JNI_CreateJavaVM returns with -1
problem of C++ Call java with JNI