gpt4 book ai didi

java - 如何使用 OSGi 片段贡献具有相同文件名的平台相关 native 代码?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:26:21 25 4
gpt4 key购买 nike

我正在使用 JNotify项目来监听文件系统事件。这取决于每个 OS:processor 架构的本地库。例如,有一个用于 Windows x86 的库,一个用于 x86-64 的库等。

单体包

最初,我有一个包含 JNotify Java 类和 native 代码的包。 native 代码在 Bundle-NativeCode 中声明如下:

(为了更好的可读性,我将它们格式化为 bnd 样式......显然实际的 MANIFEST.MF 文件格式正确)。

Bundle-NativeCode: jnotify_64bit.dll;osname=Win32;osname="Windows NT (unknown)";osname = WindowsXP;osname = Windows2000;osname = Windows2003;osname = WindowsVista;osname = Windows7;osname = WindowsServer2008;osname= Windows8;osname = WindowsServer2012;processor = x86-64,\
jnotify.dll;osname=Win32;osname="Windows NT (unknown)";osname = WindowsXP;osname = Windows2000;osname = Windows2003;osname = WindowsVista;osname = Windows7;osname = WindowsServer2008;osname = Windows8;osname = WindowsServer2012;processor = x86,\
libjnotify.so;osname = Linux;processor = x86,\
libjnotify64.so;osname = Linux;processor = x86-64,\
libjnotify.dylib;osname = Mac OSX;processor = x86;processor = x86-64,\
*

这很有效。

移动到片段

我认为如果我将库移动到单独的片段包中会“很好”,这样我就可以为我感兴趣的架构贡献片段。以 Linux 为例,我将它们分成两个包:

Linux 32 位

Include-Resource: lib/libjnotify.so
Bundle-NativeCode: libjnotify.so;osname = Linux;processor = x86,\
*
Fragment-Host: net.contentobjects.jnotify
Bundle-Version: 0.94.0

Linux 64 位

Include-Resource: lib/libjnotify.so
Bundle-NativeCode: libjnotify.so;osname = Linux;processor = x86-64,\
*
Fragment-Host: net.contentobjects.jnotify
Bundle-Version: 0.94.0

请注意,这些包是从不同的来源构建的。尽管 libjnotify.so 文件名相同,但它们在不同的 Eclipse 项目中是不同的文件。它们必须相同才能与 JNotify 一起使用。

请注意,相同的文件名会提供给主机包。在这种情况下,文件名是 libjnotify.so。

如果我在我的 64 位机器上运行这些程序,并且 64 位包在 32 位包加载之前,它就可以工作。

但是,如果先加载 32 位包,我会得到:

Couldn't initialise JNotify: java.lang.UnsatisfiedLinkError: /blah/generated/fw/bundle46/version0.0/net.contentobjects.jnotify.linux.x86-0.94.0.jar-lib/0/libjnotify.so: /blah/generated/fw/bundle46/version0.0/net.contentobjects.jnotify.linux.x86-0.94.0.jar-lib/0/libjnotify.so: wrong ELF class: ELFCLASS32 (Possible cause: architecture word width mismatch) (JnotifyFileSystemObserver.java:53, thread platformExecutor)

显然正在加载 32 位库。 但为什么呢?我的 Bundle-NativeCode 定义主机必须是 32 位 Linux 机器才能使用 32 位包的版本。我想如果那个子句不匹配,库就会被忽略?

我试过的东西

  • 删除可选的通配符...这仅意味着 bundle 无法解析

最佳答案

由于您的片段占用相同的资源命名空间,因此只能访问一个 .so 文件。您可以只部署一个片段,或者您可以尝试将它们放在不同的目录中:

 Fragment 32-bit:
Include-Resource: x32/libjnotify.so=lib/libjnotify.so

Fragment 64-bit:
Include-Resource: x64/libjnotify.so=lib/libjnotify.so

我还认为您需要将 Bundle-NativeCode header 放入主机包中并引用正确的目录,因为我认为此 header 未合并到主机中。

关于java - 如何使用 OSGi 片段贡献具有相同文件名的平台相关 native 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12912772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com