Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JNI GetMethodId return null

I have a simple issue but can not find a solution on the net.

I want simply to retrieve Java String from native using JNI.

jint JNI_OnLoad(JavaVM *vm, void *reserved) {
void * my_lib_handle;

jmethodID mid;
jstring resultString;
JNIEnv *env;
int status;

__android_log_write(ANDROID_LOG_DEBUG,"JNI", "> JNI_OnLoad");
status = (*vm)->GetEnv(vm, (void**)&env, JNI_VERSION_1_4); 
if(status < 0){
    __android_log_write(ANDROID_LOG_DEBUG,"JNI", "failed to get JNI environment");
    status = (*vm)->AttachCurrentThread(vm, (void**)&env, NULL);
    if(status < 0){
        __android_log_write(ANDROID_LOG_DEBUG,"JNI", "failed to attach to current thread");
        return;
    }
}
jclass handlerClass = (*env)->FindClass(env, "com/mypackage/JniInterface");

if (handlerClass == NULL) {
    __android_log_write(ANDROID_LOG_DEBUG,"JNI", "can not find the class JniInterface ");
    return;
}
mid = (*env)->GetMethodID(env, handlerClass, "getCert", "()Ljava/lang/String");
if (mid == NULL) {
    __android_log_write(ANDROID_LOG_DEBUG,"JNI", "can not find the method getCert");
    return;
}

FindClass works. But it is failing in GetMethodId.

Here is my simple class declaration. I made it simpler than previously :)

package com.mypackage;
public class JniInterface {
private static String cert;
private static String key;
public JniInterface(){

}
public static String getCert() {
    return cert;
}
public static void setCert(String cert) {
    JniInterface.cert = cert;
}
public static String getKey() {
    return key;
}
public static void setKey(String key) {
    JniInterface.key = key;
}   
}

I am stuck. I think it is a stupid error...

Could you please help me to investigate it ?

like image 645
user1837153 Avatar asked Nov 19 '12 22:11

user1837153


1 Answers

Your method signature is incorrect, missing a trailing semicolon:

 "()Ljava/lang/String;"    
 --------------------^
like image 132
technomage Avatar answered Nov 02 '22 23:11

technomage