java 静态包,从本地code调用另一个包的静态Java方法

For example, let say that in Android, I need to call the static method android.os.SystemClock.elapsedRealtime(), which returns a long, from a portion of native code. In the mylib.c file, I have

JNIEXPORT jlong JNICALL

Java_com_mpackage_MyClass_nativeMethod(JNIEnv *env, jobject obj){

jclass cls = (*env)->GetObjectClass(env, obj);

jmethodID mid = (*env)->GetStaticMethodID(env, cls,

"android.os.SystemClock.elapsedRealtime", "(V)J");

if (mid == 0)

return 0L;

return CallStaticLongMethod(cls, mid);

}

In the java MyClass.class, I have among others

static {System.loadLibrary("myLib");}

native long nativeMethod();

but when I call it, I get

ERROR/AndroidRuntime(628): java.lang.NoSuchMethodError:

android.os.SystemClock.elapsedRealtime()

at the declaration of mid line. I think this is straightforward but I'm new to jni. Can someone point out my mistake(s) ?

解决方案

Looks like your usage of the JNI API is not proper.

First you should get the class reference of android.os.SystemClock. The obj passed as a parameter, is an object of MyClass. You should use (*env)->FindClass(env, "android/os/SystemClock") to get a jclass for the SystemClock. Then call (*env)->GetStaticMethodID(env, cls,"elapsedRealtime", "(V)J"); to get the method id. Take a look at the JNI tutorial for further details

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值