1.步骤
1.编写dll文件,放入项目的bin目录(在window上是dll文件,在Linux上是so文件,dll和so都是由C程序生成)
2.新建接口继承Library
3.加载对应的dll或者so文件:CLibrary INSTANCE =Native.loadLibrary("hz", CLibrary.class);
4.声明native方法:void HelloWorld();
5.调用 :CLibrary.INSTANCE.HelloWorld();
2.一个简单实例
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform; public class HelloWord { public interface CLibrary extends Library{
//加载库
CLibrary INSTANCE =Native.loadLibrary("hz", CLibrary.class);
//声明方法
void HelloWorld();
} public static void main(String[] args) {
//调用
CLibrary.INSTANCE.HelloWorld();
}
}
3.资料整理
JNA3.2.7的API:http://api.suwish.com/jna/
JNA4.4.0的API:http://java-native-access.github.io/jna/4.4.0/javadoc/
JNA的GitHub:https://github.com/java-native-access/jna
参数对照表在API中即可查看。