"JNIEnv *env" doesn't contain MY Java classes in native DLL's non-primary Thread - unable to call back Java methods. CLASSPATH problem. Why?
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Amit_Rosner
Posted On:   Wednesday, October 17, 2001 03:23 AM

Hi! This is a very strange problem. I write a swing Applet that uses JNI. In the native DLL I keep a global variable "JavaVM *jvm;", that is initialized only once in a Java native "connect" method from the "JNIEnv *env" parameter with "env->GetJavaVM(&jvm);". That method (and other Java native methods that are implemented in the native DLL) is called of course in the primary Thread. There I can easily find my defined Java classes with "env->FindClass(...);" and call back the Java methods. In the "connect" method, after initializing the "JavaVM *jvm" pointer, I start another Thread in    More>>

Hi!



This is a very strange problem. I write a swing Applet that uses JNI. In the native DLL I keep
a global variable "JavaVM *jvm;", that is initialized only once in a Java native "connect" method
from the "JNIEnv *env" parameter with "env->GetJavaVM(&jvm);".



That method (and other Java native methods that are implemented in the native DLL) is called
of course in the primary Thread. There I can easily find my defined Java classes with "env->FindClass(...);"
and call back the Java methods.



In the "connect" method, after initializing the "JavaVM *jvm" pointer, I start another Thread in
the DLL, and then return to the Applet to continue its execution. Meanwhile, the second Thread does some
operation and should call back some Java static method, using the standard callback procedure described here:



// I am in some CPP method in the DLL.



JNIEnv *env;

JavaVMAttachArgs args;



jvm->AttachCurrentThread((void **)&env, &args);



jclass cls = env->FindClass(...);

jmethodID callback = env->GetStaticMethodID(cls, ..., ...);



env->CallStaticVoidMethod(cls, callback, ...);



jvm->DetachCurrentThread();



If I run the Applet from within standard developing environment, such as JBuilder (or any other),
everything works just fine. The problem begins when I want to run the Applet in MS IExplorer or Netscape
(I have installed the swing plug-in successfully), as well as Sun's AppletViewer.



The problem is that the "JNIEnv *env" pointer, that is initialized with "jvm->AttachCurrentThread((void **)&env, &args)"

DOES NOT CONTAIN ANY OF MY DEFINED JAVA CLASSES.

However, it contains all of the standard java classes, such as "java.lang.String" and so on.
The result of that is that I am unable to find defined Java classes and call back Java methods from
the DLL in non-primary Threads.



When I looked in JBuilder, to study how does it run the Applet, I saw that it invokes "javaw" with
the switch "-classpath MyApplet.jar". Now I tried to call AppletViewer from the command prompt I got the
following results:



MyApplet.jar --- The jar containing the applet and all other files.

index.html --- The html file that points to the MyApplet.jar file.



I called :
"javaw -classpath MyApplet.jar sun.applet.AppletViewer index.html".

The result was good - everything works properly.



I called :
"javaw sun.applet.AppletViewer index.html" (without classpath).

The result was as described above - the "JNIEnv *env" pointer in the non-primary Thread didn't contain any
of my defined Java classes and so no call to a callback Java method was performed.



Obviously the problem resides in the classpath. How can I solve it?    <<Less
About | Sitemap | Contact