JNI
package org.jxch.study;
public class Demo {
static {
System.loadLibrary("DemoNative");
}
private native void demo();
// 回调 Java 方法
private void callback() {
System.out.println("callback");
}
}
编译头文件 javac -h . Demo.java
编写 demo.c
:
#include "org_jxch_study_Demo.h"
org_jxch_study_Demo_demo(JNIEnv *env, jobject c1) {
printf("netive demo/n");
jclass thisClass = (*env)->GetObjectClass(env, c1);
//根据方法名和签名获取方法的id
jmethodID midCallBack = (*env)->GetMethodID(env, thisClass, "callback", "()V");
//调用方法
(*env)->CallVoidMethod(env, c1, midCallBack);
}
生成 so 文件:
gcc -fPIC -I /...jdk.../include -I /...jdk.../include/linux -shared -o DemoNative.so demo.c
添加到环境变量:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/...demo.c 所在目录...