java - JNI - problem with threads and jobject -


I have said a native program that creates another thread, which adds itself to JVM. Now I want to reach JVM's methods, but it fails. Here's the code:

  // // This is the original work that is called first. // It's running which creates another thread, and also calls printing-methods in my // Java applet. // JNIEXPORT void JNICALL Java_EIGC_1Applet_app_1_native_native_1start (JNIEnv * jenv, job job) {printAppletConsole (JONV, job, "unused ATM"); // Save current Java VM; // save the main applet class; // Used by the main thread jenv-> GetJavaVM (& amp; applet_java_jvm); M_job = Job; // Start working and start it applet_thread_main = new EIGC_Applet_thread (& amp; main); Applet_thread_main- & gt; Start (); } // // This is the running thread that was created // it will float and will call the printing method in the applet // unsigned __stdcall main (zero * args) {// JNIEnv JNIEnv * jenv = new JNIEnv (); // JVM applet_java_jvm- & gt; Attach thread to run attachract current trudge (zero **) jnew, tap); // main floating loop while (true) {sleep (1000); PrintAppletConsole (jenv, m_job, "unused"); } Applet_thread_main-> Stop (); Return 0; } // // call "Writing Console ()" method in applet, which prints "test" in a JTextArea // void printAppletConsole (JNIEnv * jenv, job job, four * text) {jclass cls = jenv - & gt; GetObjectClass (job); JmethodID middle = jenv- & gt; GetMethodID (CLS, "Writing Console", "() V"); If (middle == faucet) {printf ("method not found"); } And {Janew-> Callwhite mode (Job, Middle); }}   

I have a question;

1) In the newly created thread, JVM just hangs over GetObjectClass when I try to call PrintAppletConsole Why is it like this?

My suspicion is that since I have created a new thread, I have to use a new example of job, but I am not sure how .. ..

Thank you!

  m_job = job;   

It only holds local references that whenever you return to Java, it is invalid. You need to create a global context with NewGlobalRef and store it.

  JNIEnv * jenv = new JNIEnv (); Applet_java_jvm- & gt; AttachCurrentThread ((zero **) jenv, zero);   

should be:

  JNIEnv * jenv = 0: applet_java_jvm- & gt; Attachment Present (& amp; jenv, NULL); Edit: For older JNI versions, use:  
  JNIEnv * jenv = 0: applet_java_jvm- & gt; Attachment creditTrade (zero **) and genew, zero);    

Comments