There is such java code:

public class HelloJni extends Activity { public void on_connected(Boolean ok) {} }; 

from NDK I try to call this method like this:

 JNIEXPORT void JNICALL connect(JNIEnv *env, jclass) { jclass cls = env->FindClass("com/example/hellojni/HelloJni"); jmethodID mid = env->GetMethodID(cls, "on_connected", "(Z)V"); env->CallVoidMethod(cls, mid, (jboolean)false); } 

but I get:

JNI DETECTED ERROR IN APPLICATION: JNI CallVoidMethodV called with pending exception java.lang.NoSuchMethodError: no non-static method "Lcom / example / hellojni / HelloJni; .on_connected (Z) V"

Don't understand what I'm doing wrong?

And yet, it is not clear why in the error message the method name looks like this (that is, with a comma): "Lcom/example/hellojni/HelloJni;.on_connected(Z)V"

  • I DO NOT know why, but if I change the type of the variable ok from Boolean to int and, accordingly, jmethodID mid = env-> GetMethodID (cls, "on_connected", "(Z) V"); env-> CallVoidMethod (cls, mid, (jboolean) false); on jmethodID mid = env-> GetMethodID (cls, "on_connected", "(I) V"); env-> CallVoidMethod (cls, mid, 0); That does not fall - Oleg A
  • one
    Z is boolean , not Boolean . Try replacing the boolean part of a boolean in Java (that is, a primitive). - Suvitruf
  • @suvitruf, yes you are right! Most likely, Mr. Tasheehoobv’s precisely this was the mistake - Oleg A

1 answer 1

I haven't worked with jni for a long time, but if my memory serves me, then the method name in the jni part should have the full path. That is, in your case:

 JNIEXPORT void JNICALL com_example_hellojni_HelloJni_connect(JNIEnv *env, jclass) { jclass cls = env->FindClass("com/example/hellojni/HelloJni"); jmethodID mid = env->GetMethodID(cls, "on_connected", "(Z)V"); env->CallVoidMethod(cls, mid, (jboolean)false); } 

connect same method class HelloJni ?

Plus, in your case it will work only within the method. When referring to links outside of this method, there will be NULL 's. It is better to get a reference to the JVM via env->GetJavaVM(&mJVM) , if in the future you want to call this method outside com_example_hellojni_HelloJni_connect .

I wrote an article about it in my time. I think it is still relevant.

Also , Z is boolean , not Boolean . Try replacing the boolean part of a boolean in Java (that is, a primitive).

  • yes, about com_example_hellojni_HelloJni_connect - you are right. I shortened the name in the example. about the article - thanks! - tasheehoo