title: JNI Callback to Java using Interface
body: <p>First of all, let me list the best result I could fetch.
  <a href="https://stackoverflow.com/questions/23223058/jni-call-java-method-which-take-a-custom-java-interface-as-parameter">jni call java method which take a custom java interface as parameter</a></p>
  
  <p>This does not answer mine. Let me explain my problem. I want to make a call to NDK as follows.</p>
  
  <p>(1)Java -> (2)CPP -> (3)C (new thread) -> (4)CPP -> (5)Java</p>
  
  <p>Code is below.</p>
  
  <p>(1) Java</p>
  
  <pre><code>public interface Callback&lt;T&gt; {
      void success(T result);
  }
  private native void jniUploadAsync(String imagePath, Callback&lt;String&gt; callback);
  
  jniUploadAsync(file.getAbsolutePath(), new Callback&lt;String&gt;() {
                                          @Override
                                          public void success(final String result) {
                                              Log.v("MyClass: result:: ", result);
                                          }
                                      });
  </code></pre>
  
  <p>(2) CPP</p>
  
  <pre><code>static JavaVM *jvm;
  void imageUploadCallback(char *json, void *completionCallback) {
      JNIEnv *env;
      jint rs = jvm-&gt;AttachCurrentThread(&amp;env, NULL);//create JNIEnv from JavaVM    
      jclass cbClass = env-&gt;FindClass("org/winster/test/Callback");
      jmethodID method = env-&gt;GetMethodID(cbClass, "success", "(Ljava/lang/String;)V");
      env-&gt;CallVoidMethod(static_cast&lt;jobject&gt;(completionCallback), method, "abcd");
  }
  
  void Java_org_winster_test_MyClass_jniUploadAsync(JNIEnv * env, jobject obj, jstring imagePath, jobject completionCallback) {
      jint rs = env-&gt;GetJavaVM(&amp;jvm); //Cache JavaVM here
      CallMyCMethod((char *)filePath, &amp;imageUploadCallback, &amp;completionCallback);
  }
  </code></pre>
  
  <p>(3) C</p>
  
  <pre><code>CallMyCMethod() //please assume that it works. The reason I need void* as the type for completionCallback is because, in ObjC implementation I use this
  </code></pre>
  
  <p>(4) CPP</p>
  
  <pre><code>//Call comes back to imageUploadCallback()
  </code></pre>
  
  <p>(5) Java</p>
  
  <pre><code>//I expect this Log.v("MyClass: result:: ", result); to be executed
  </code></pre>
  
  <p>Please note that, this is not a basic question about how to call Java from C++. The 2 specific points I want to resolve is, how to call the "callback" and how to invoke a method in a Java Interface implementation. I have done this for Obj-C where it is straight forward.</p>