答案 0 :(得分:2)
是的,可以像往常一样构建和编译共享库,并使用Native.loadLibrary
加载它。
<强> C:强>
#include <stdio.h>
void exampleMethod(const char* value)
{
printf("%s\n", value);
}
以通常的方式编译它(在linux上显示gcc):
gcc -c -Wall -Werror -fPIC test.c
gcc -shared -o libtest.so test.o
<强>爪哇:强>
import com.sun.jna.Library;
import com.sun.jna.Native;
public class TestJNA{
public interface CLibrary extends Library {
public void exampleMethod(String val);
}
public static void main(String[] args) {
CLibrary mylib = (CLibrary)Native.loadLibrary("test", CLibrary.class);
mylib.exampleMethod("ImAString");
}
}
由于您在查找库时遇到问题,因此通常会修复配置java.library.path
添加新位置,其中将搜索.so / .dll:
java -Djava.library.path=c:\dlllocation TestJNA
或者您可以在加载库之前直接从代码中设置它(与JNI一起使用,也应该使用JNA,但我没有尝试过):
String libspath = System.getProperty("java.library.path");
libspath = libspath + ";c:/dlllocation";
System.setProperty("java.library.path",libspath);
//Load your library here
答案 1 :(得分:0)
对于jna的“uraimo”的回答,它应该使用jna.library.path而不是java.library.path,这应该解决位置问题。