无法在Maven项目中加载库
我有一个第三方本地库(我们称之为 test.dylib ),我试图通过Maven项目中的JNA加载。 otool -l test.dylib 的输出是
/Library/Frameworks/test/Versions/A/test (compatibility version 1.0.0, current version 1.0.0)
/System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa (compatibility version 1.0.0, current version 19.0.0)
/System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 945.11.0)
/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 65.1.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 169.3.0)
/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 744.1.0)
如果我在位置粘贴test.dylib文件”/library/frameworks/test/test/a/a/a/test” ” - L输出。 并通过JNA加载文件,它可以正常工作。但是,如果我将test.dylib保留在资源文件夹下,则会出现错误。
java.io.io exception:本机库(darwin-x86-64/test.dylib)在资源路径(/users users/username/downloads/testjna/target/class/class:
即使我可以看到/target中存在的dylib文件, /类别的文件夹,我可以从资源文件夹中拨打Dylib吗
?正在加载本地库。
public class TestJNA {
public interface Perception extends Library {
Perception Instance = (Perception) Native.load("test", Perception.class);
void method1();
int method2(int a, int b, int c);
}
public static void main(String[] args) {
Perception.Instance.method1();
System.out.println(Perception.Instance.method2(1,2,3));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您收到的错误消息仅列出一个搜索路径时,JNA实际上检查了您的库的多个位置。如:
请注意上面项目4中的
OS-PREFIX
目录。您已将库直接包含在classPath(从注释,/users/username/downloads/testjna/src/src/main/java/test.dylib
)上包含。但是,正如错误消息所述,它需要在您的classPath上的darwin-x86-64/
目录中,例如,/users/username/downloads/testjna/src/main// java/darwin-x86-64/test.dylib
;您可能希望使用
src/main/resources
优先使用java
非Java代码的子目录。您将系统库路径设置为原始源代码目录的尝试不适用于在
target/class/class
目录结构中查看的编译代码。请注意,您可以通过设置系统属性
jna.debug_load = true
来获取进一步的调试信息。While the error message you received only lists one search path, JNA actually checks multiple locations for your library. As documented in the NativeLibrary class javadocs:
Note the
os-prefix
directory in item 4 above. You have included your library directly on the classpath (from the comments,/Users/username/Downloads/TestJNA/src/main/java/test.dylib
). However, as the error message states, it needs to be in thedarwin-x86-64/
directory on your classpath, e.g.,/Users/username/Downloads/TestJNA/src/main/java/darwin-x86-64/test.dylib
;You may wish to use
src/main/resources
in preference to thejava
subdirectory for non-java code.Your attempt to set the system library path to your raw source code directory doesn't work for the compiled code where it is looking in the
target/classes
directory structure.Note that you can get further debugging information by setting the system property
jna.debug_load=true
.