[英]Java, JNI and Class declarations
Can any JNI expert explain the following situation, as i cannot wrap my head around the issue. 任何JNI专家都可以解释以下情况,因为我无法解决这个问题。
Say we have this class: 说我们有这个课:
public class MyClass {
static {
System.loadLibrary("recorder");
}
private native long function1();
private native void function2();
private void callback() {
}
public static void main(String[] args) throws Exception {
MyClass obj1 = new MyClass();
obj1.function1();
}
}
in JNI (C): 在JNI(C)中:
JNIEXPORT jstring JNICALL function1(JNIEnv *env, jobject object) {
jclass cls = (*env)->GetObjectClass(env, object);
jmethodID mid = (*env)->GetMethodID(env, cls, "callback", "()V");
if (mid != NULL) {
(*env)->CallVoidMethod(env, object, mid);
}
}
This works fine, however, if instead of 但是,如果不是
private native long function1();
i declare 我宣布
private static native long function1();
the call from JNI to Java fails complaining it cannot find the function callback in Java. 从JNI到Java的调用失败,抱怨它找不到Java中的函数回调。
Thanks 谢谢
For static native method the second argument in native code should not be the jobject
but jclass
. 对于静态本机方法,本机代码中的第二个参数不应为
jobject
而应为jclass
。
But you should rather generate c headers with javah
tool rather than writing them manually. 但是,您应该使用
javah
工具生成c标头,而不要手动编写它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.