Java jna.loadLibrary找不到本机库文件
我已经用本机库包装器创建了自己的jar。结果jar的结构是:Java jna.loadLibrary找不到本机库文件,java,native,jna,unsatisfiedlinkerror,Java,Native,Jna,Unsatisfiedlinkerror,我已经用本机库包装器创建了自己的jar。结果jar的结构是: library.jar |- com (there are my .java classes) |- libs (there is the native - libmylib.so) |- META-INF 我按如下方式加载本机库: MyLibClass instance = (MyLibClass) Native.loadLibrary("mylib", MyLibClass.class); static class Lo
library.jar
|- com (there are my .java classes)
|- libs (there is the native - libmylib.so)
|- META-INF
我按如下方式加载本机库:
MyLibClass instance = (MyLibClass) Native.loadLibrary("mylib", MyLibClass.class);
static class Loader {
private Loader() {
}
static String getNative() {
InputStream in = null;
FileOutputStream fos = null;
File fileOut = null;
System.setProperty("jna.library.path",
System.getProperty("java.io.tmpdir"));
in = Loader.class.getResourceAsStream(
"/libs/libmylib.so");
if (in != null) {
try {
fileOut = File.createTempFile("mylib", ".so");
fileOut.deleteOnExit();
fos = new FileOutputStream(fileOut);
int count;
byte[] buf = new byte[1024];
while ((count = in.read(buf, 0, buf.length)) > 0) {
fos.write(buf, 0, count);
}
} catch (IOException ex) {
throw new Error("Failed to create temporary file: " + ex);
} finally {
try {
in.close();
} catch (IOException ex) {
}
if (fos != null) {
try {
fos.close();
} catch (IOException ex) {
}
}
return fileOut.getAbsolutePath();
}
} else {
throw new Error("Couldn't open native library file");
}
}
}
现在我想在其他项目中添加这个库并使用它。但当我创建MyLibClass的实例时,我收到一个错误:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'mylib':
libmylib.so: cannot open shared object file: No such file or directory
如何解决此问题?如页面所述
使目标库可用于Java程序。有几种方法可以做到这一点:
- 首选方法是将
系统属性设置为目标库的路径。此属性类似于jna.library.path
,但仅适用于JNA加载的库java.library.path
- 在启动VM之前,请更改相应的库访问环境变量。这是Windows上的
,Linux上的PATH
,OSX上的LD\u LIBRARY\u PATH
DYLD\u LIBRARY\u PATH
- 在类路径下,在路径
下,使本机库可用,其中{OS}-{ARCH}/{library}
是JNA本机库的规范前缀(例如{OS}-{ARCH}
,win32-x86
,或linux-amd64
)。如果资源位于jar文件中,则在加载时将自动提取该资源darwin
MyLibClass instance = (MyLibClass) Native.loadLibrary("mylib", MyLibClass.class);
static class Loader {
private Loader() {
}
static String getNative() {
InputStream in = null;
FileOutputStream fos = null;
File fileOut = null;
System.setProperty("jna.library.path",
System.getProperty("java.io.tmpdir"));
in = Loader.class.getResourceAsStream(
"/libs/libmylib.so");
if (in != null) {
try {
fileOut = File.createTempFile("mylib", ".so");
fileOut.deleteOnExit();
fos = new FileOutputStream(fileOut);
int count;
byte[] buf = new byte[1024];
while ((count = in.read(buf, 0, buf.length)) > 0) {
fos.write(buf, 0, count);
}
} catch (IOException ex) {
throw new Error("Failed to create temporary file: " + ex);
} finally {
try {
in.close();
} catch (IOException ex) {
}
if (fos != null) {
try {
fos.close();
} catch (IOException ex) {
}
}
return fileOut.getAbsolutePath();
}
} else {
throw new Error("Couldn't open native library file");
}
}
}
在那里,我从资源中加载库文件,并将其内容复制到临时目录。正如您在执行此操作之前所看到的,我将jna.library.path设置为temp文件夹,以便jna在那里搜索库
此外,我正在加载库,如下所示:
MyLibClass instance = (MyLibClass) Native.loadLibrary(Loader.getNative(), MyLibClass.class);