1.构建项目
准备最简单的maven项目,结构如下:
引入依赖:
<dependency><groupId>net.java.dev.jna</groupId><artifactId>jna</artifactId><version>5.3.1</version></dependency>
2.构建c文件和so文件
在resource目录下构建:
#include <stdio.h>test() {printf("hello world\n");
}
编译生成so:
gcc test.c -fPIC -shared -o test.so
3.编写解析类和测试类
package org.linx;import com.sun.jna.Library;
import com.sun.jna.Native;public interface CLibrary extends Library {CLibrary INSTANCE = Native.loadLibrary("test.so", CLibrary.class);/*** 初始化SDK 注意:调用SDK其他接口前必须先调用此接口!*/void test();}
package org.linx;import junit.framework.TestCase;
import org.junit.Test;public class CLibraryTest extends TestCase {@Testpublic void testTest() {CLibrary.INSTANCE.test();}
}