ddas_auth
asked on
FindClass fails in JNI call from C++
I cannot load any java class from C++ program.
Following is my code.
JavaVM *jvm; /* denotes a Java VM */
JNIEnv *env; /* pointer to native method interface */
JavaVMInitArgs vm_args;
JavaVMOption options[2];
/* disable JIT */
options[0].optionString ="-Djava.compiler=NONE";
/* user classes */
options[1].optionString =
"-Djava.class.path=C:\\Pro gram Files\\Java\\jdk1.5.0_11\\ jre\\bin\\ client;C:\ \Program Files\\Java\\jdk1.5.0_11\\ jre\\bin";
vm_args.version = JNI_VERSION_1_4;
vm_args.options = options;
vm_args.nOptions = 2;
vm_args.ignoreUnrecognized = TRUE;
jint res = JNI_CreateJavaVM(&jvm, (void **)&env, &vm_args);
if (res < 0) {
printf("Couldn't create JVM. %s", &res);
}
printf("JVM is created");
jclass cls = env->FindClass("java/lang/ String"); //fails here
Thanks in adance for your help. I have read many discussions on JNI. It seems I am doing something wrong.
Following is my code.
JavaVM *jvm; /* denotes a Java VM */
JNIEnv *env; /* pointer to native method interface */
JavaVMInitArgs vm_args;
JavaVMOption options[2];
/* disable JIT */
options[0].optionString ="-Djava.compiler=NONE";
/* user classes */
options[1].optionString =
"-Djava.class.path=C:\\Pro
vm_args.version = JNI_VERSION_1_4;
vm_args.options = options;
vm_args.nOptions = 2;
vm_args.ignoreUnrecognized
jint res = JNI_CreateJavaVM(&jvm, (void **)&env, &vm_args);
if (res < 0) {
printf("Couldn't create JVM. %s", &res);
}
printf("JVM is created");
jclass cls = env->FindClass("java/lang/
Thanks in adance for your help. I have read many discussions on JNI. It seems I am doing something wrong.
classpath look wrong, it should point to where your classes are
Hey,
are u getting compile time error ?
if it is please verify the FindClass signature !!, I think it is "jclass FindClass(JNIEnv *env, const char *name);"
Regards,
Prashant Sabnekar
are u getting compile time error ?
if it is please verify the FindClass signature !!, I think it is "jclass FindClass(JNIEnv *env, const char *name);"
Regards,
Prashant Sabnekar
ASKER
Well I failed to load any of my classes, hence I am trying to load java.lang.String which is part of standard java.
ASKER
I am not getting any compile time error.
After I removed the jvm.dll from Windows\System32 directory, I get the message "The application has failed to start because jvm.dll was not found..."
After I removed the jvm.dll from Windows\System32 directory, I get the message "The application has failed to start because jvm.dll was not found..."
ASKER
As soon as I added jvml.dll location in the client jre to the path env variable it started working. There some information about this in the following link.
http://java.sun.com/products/archive/j2se/1.4.2_05/install-windows.html
All I had to do was set path=%path%;C:\Program Files\Java\jre1.5.0_11\bin \client;
Thanks everyone for responding.
http://java.sun.com/products/archive/j2se/1.4.2_05/install-windows.html
All I had to do was set path=%path%;C:\Program Files\Java\jre1.5.0_11\bin
Thanks everyone for responding.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
1) I have included jvm.lib in my project from the floowing location.
C:\Program Files\Java\jdk1.5.0_11
2) I have the following pragma in a cpp file
#pragma comment(lib, "jvm.lib")
3) I copied jvm.dll into C:\Windows\System32 directory