Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JNI NewStringUTF called with pending exception 'java.lang.NoSuchMethodError'

I don't know why exception is being thrown when I call native method.

Native code :

JNIEXPORT jstring JNICALL Java_org_amk_lockscreen_JNIActivity_getPackege(JNIEnv *env,jclass cls,jobject context){
    jclass Context_class = (*env).FindClass("android/content/Context");
    jmethodID midGetPackageName = (*env).GetMethodID(Context_class, "getPackageName", "(V)Ljava/lang/String;");
    if(midGetPackageName==0){
        return (*env).NewStringUTF("Not Found Method");
    }
    else{
        jobject packageName=(*env).CallObjectMethod( context, midGetPackageName);
        return (jstring)packageName;
    }
}

and my java code:

public class JNIActivity extends Activity {

    static {
        System.loadLibrary("myjni"); // "myjni.dll" in Windows, "libmyjni.so" in Unixes
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jni);
        TextView txt = (TextView) findViewById(R.id.txtMessage);
        try {
            txt.setText(getPackege(getApplicationContext()));
        } catch (Exception exp) {
            txt.setText(exp.getMessage());
        }
    }

    public native String getMessage();

    public native String getPackege(Context con);
}

but when run this, it throws exception:

JNI DETECTED ERROR IN APPLICATION: JNI NewStringUTF called with pending exception 'java.lang.NoSuchMethodError' thrown in java.lang.String org.amk.lockscreen.JNIActivity.getPackege(android.content.Context):-2
in call to NewStringUTF
from java.lang.String org.amk.lockscreen.JNIActivity.getPackege(android.content.Context)

I have tested different method signature, like:

(Ljava/lang/Void;)Ljava/lang/String;     
   (V;)Ljava/lang/String;

but it throws exception again.

Where I made a mistake?

like image 667
ali kiani Avatar asked Oct 24 '25 15:10

ali kiani


1 Answers

GetMethodID is raising an exception. You are not checking to see if an exception was raised with env->ExceptionCheck(env) then clearing it with ExceptionClear() or returning immediately from the function so the JVM handles it. See the Oracle doc on getMethodId here: http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/functions.html

For example (from http://www.ibm.com/developerworks/library/j-jni/:) findClass, GetStaticMethodID and CallStaticVoidMethod call all generate exceptions similar to GetMethodID.

int val=1;
jmethodID method;
jclass cls;

cls = (*env)->FindClass(env, "com/ibm/example/TestClass");
if ((*env)->ExceptionCheck(env)) {
   return ERR_FIND_CLASS_FAILED;
}
method = (*env)->GetStaticMethodID(env, cls, "setInfo", "(I)V");
if ((*env)->ExceptionCheck(env)) {
   return ERR_GET_STATIC_METHOD_FAILED;
}
(*env)->CallStaticVoidMethod(env, cls, method,val);
if ((*env)->ExceptionCheck(env)) {
   return ERR_CALL_STATIC_METHOD_FAILED;
}
like image 97
JJF Avatar answered Oct 26 '25 04:10

JJF



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!