无法在Maven项目中加载库

发布于 2025-01-30 06:01:25 字数 1658 浏览 4 评论 0 原文

我有一个第三方本地库(我们称之为 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));
    }
}

I have a 3rd party native library (let's call it test.dylib) which I am trying to load through JNA in a Maven project.
output of otool -L test.dylib is

/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)

If I paste the test.dylib file at location "/Library/Frameworks/test/Versions/A/test " from otool -L output.
And load the file through JNA, it works fine. But if I keep the test.dylib under resource folder, I get error.

java.io.IOException: Native library (darwin-x86-64/test.dylib) not found in resource path (/Users/username/Downloads/TestJNA/target/classes:

Even though I can see the dylib file present in /target/classes folder. Can someone suggest, how can I achieve calling dylib from resource folder? I don't want to paste the dylib file at this location "/Library/Frameworks/test/Versions/A/test".

This is how I am loading native library.

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

还在原地等你 2025-02-06 06:01:25

当您收到的错误消息仅列出一个搜索路径时,JNA实际上检查了您的库的多个位置。如

搜索给定库将扫描以下位置:

  1. jna.library.path 用户可定制路径
  2. jna.platform.library.path 平台特定路径
  3. 在OSX上,〜/library/Frameworks /library/Frameworks /system/library/frameworks 将被搜索带有框架
    名称与所请求的相对应。框架的绝对路径是
    也接受了,要么以框架名称结尾(sans“ .framework”)
    或框架共享库的完整路径(例如
    coreservices.framework/coreservices)。
  4. 上下文类加载程序类路径。可以在 $ {os-prefix}/library_filename 的基础路径上安装部署的本机库
    其中 $ {OS-PREFIX} 是OS/ARCH前缀返回的
    platform.getNativelibraryResourceprefix()。如果捆绑在罐子文件中,
    该资源将提取到 jna.tmpdir 用于加载,然后再提取
    删除(但仅当 jna.nounpack 是false或不设置时)。

您可以设置系统属性 jna.debug_load = true 制作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:

A search for a given library will scan the following locations:

  1. jna.library.path User-customizable path
  2. jna.platform.library.path Platform-specific paths
  3. On OSX, ~/Library/Frameworks, /Library/Frameworks, and /System/Library/Frameworks will be searched for a framework with a
    name corresponding to that requested. Absolute paths to frameworks are
    also accepted, either ending at the framework name (sans ".framework")
    or the full path to the framework shared library (e.g.
    CoreServices.framework/CoreServices).
  4. Context class loader classpath. Deployed native libraries may be installed on the classpath under ${os-prefix}/LIBRARY_FILENAME,
    where ${os-prefix} is the OS/Arch prefix returned by
    Platform.getNativeLibraryResourcePrefix(). If bundled in a jar file,
    the resource will be extracted to jna.tmpdir for loading, and later
    removed (but only if jna.nounpack is false or not set).

You may set the system property jna.debug_load=true to make JNA
print the steps of its library search to the console.

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 the darwin-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 the java 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文