1. 编写Java程序(JMain.java)
class JMain {
static {
System.loadLibrary("tjni");
}
public static void main(String[] args) {
System.out.println(tjni(100));
}
public native static int tjni(int i);
}
2. 编译Java程序并生成头文件
javac JMain.java
javah JMain
3. 用C++实现
#include "JMain.h"
JNIEXPORT jint JNICALL Java_JMain_tjni(JNIEnv *, jclass, jint i)
{
return i;
}
4. 编译成动态库
g++ -shared -o libtjni.so JMain.cpp
5. 配置动态库路径
export LD_LIBRARY_PATH=/home/robinmei/java/jni/
5. 运行
java JMain