Name: joT67522 Date: 01/09/98
The following code fails to find my class UNLESS I put my class
in a classes.jar and put the classes.jar in the JDK1.1.5\lib directory.
This is the case no matter what I set my CLASSPATH
environment variable to. I tried pointing CLASSPATH at my classes.jar
(with a different name) to no avail. Finally, while running my code under the
Visual C++ debugger, I finally looked at the classpath variable value in
the JDK1_1InitArgs structure which gave me the idea of using one of those
non-existent jar files. It seems, as a guess, that the FindClass routine is using the default,
internal, CLASSPATH search value and ignoring the environment variable.
(note that this code was copied from your example invoke.c with very little
modification.)
static long initJava()
{
JNIEnv *env;
JavaVM *jvm;
JDK1_1InitArgs vm_args;
jint res;
jclass cls;
jmethodID mid;
jstring jstr;
jobjectArray args;
jthrowable exc;
/* IMPORTANT: specify vm_args version # if you use JDK1.1.2 and beyond */
vm_args.version = 0x00010001;
JNI_GetDefaultJavaVMInitArgs(&vm_args);
res = JNI_CreateJavaVM(&jvm, &env, &vm_args);
if ( res < 0 )
{
fprintf(stderr, "Can't create Java VM\n");
exit(1);
}
cls = env->FindClass(ODBCUADPCLASS);
if ( cls == 0 )
{
exc = env->ExceptionOccurred();
if ( exc )
{
env->ExceptionDescribe();
env->ExceptionClear();
}
else
{
fprintf(stderr, "No exception info\n");
}
fprintf(stderr, "Can't find " ODBCUADPCLASS " class\n");
exit(1);
}
mid = env->GetStaticMethodID(cls, "main", "([Ljava/lang/String;)V");
if ( mid == 0 )
{
fprintf(stderr, "Can't find " ODBCUADPCLASS ".main\n");
exit(1);
}
jstr = env->NewStringUTF(" from C!");
if ( jstr == 0 )
{
fprintf(stderr, "Out of memory\n");
exit(1);
}
args = env->NewObjectArray(1, env->FindClass("java/lang/String"), jstr);
if ( args == 0 )
{
fprintf(stderr, "Out of memory\n");
exit(1);
}
env->CallStaticVoidMethod(cls, mid, args);
jvm->DestroyJavaVM();
return (0);
}
(Review ID: 22255)
======================================================================