title: How to implement a callback to Java from C using JNI?
body: <p>I use JNI in my Java code to call native C code. This works fine and I'm doing some processing in the C code. The C code registers an event listener so that I'm notified every time a new result is ready in the C part.</p>
  
  <p>Now I want to return the results to the Java code like that:</p>
  
  <pre><code>void notifyGazeEvent()
  {
      jclass cls = (*env)-&gt;GetObjectClass(env, obj);
      jmethodID mid = (*env)-&gt;GetMethodID(env, cls, "newGazeEvent", "(I)V");
      if (mid == 0)
      {
          return; 
      }
      printf("In C, about to enter Java");
      (*env)-&gt;CallVoidMethod(env, obj, mid, 1);
  }
  </code></pre>
  
  <p>The problem now is: I don't know where to get the JNIEnv object and the jObject object from. This is passed when I first call the C code:</p>
  
  <pre><code>JNIEXPORT void JNICALL
  Java_a_b_C_doCalculation(JNIEnv *env, jobject obj)
  {
  // here I register all the handlers etc, 
  }
  </code></pre>
  
  <p>But I don't know how I can pass the references until my method that does the callback to Java.</p>