将原生 dll 与 jar 捆绑在一起
可能的重复:
如何捆绑本机库和 JAR 内的 JNI 库?
我需要将本机库(jnotify,但我认为这并不重要)包含到我的 jar 中。我想用 NetBeans 来实现。
我添加了Bundle-NativeCode:/lib/jnotify.dll; osname=win32 添加到我的 manifest.mf
文件中,并将 jnotify.dll
添加到 projektHome\src\lib\
文件夹中。但不幸的是 NetBeans 覆盖了 manifest.mf
文件。
我该如何修复?我可以仅使用 NetBeans 来完成此操作吗?是行 'Bundle-NativeCode: /lib/jnotify.dll; osname=win32 正确吗?我还听说我应该将 dll 哈希放入
manifest.mf
并签署我的 jar。这是真的吗?
Possible Duplicate:
How to bundle a native library and a JNI library inside a JAR?
I need to include native lib (jnotify but I think that it does't matter) to my jar. I want to do it with NetBeans.
I added Bundle-NativeCode: /lib/jnotify.dll; osname=win32
to my manifest.mf
file and added jnotify.dll
to projektHome\src\lib\
folder. But unfortunately NetBeans is overidning manifest.mf
file.
How can I fixed? Can I do this using only NetBeans? Is it line 'Bundle-NativeCode: /lib/jnotify.dll; osname=win32
correct? I also heard I should put dlls hash in manifest.mf
and sign my jar. Is that true?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为 Java 可执行文件不支持
Bundle-NativeCode
。我很确定这是一个 OSGi 属性。支持的属性列表在JAR 文件规范<中定义/a>.在提供它的外部框架中,没有内置支持在 JAR 文件内捆绑本机库。如果我没记错的话,可以将文件提取到临时位置并手动加载。
I don't think the Java executable supports
Bundle-NativeCode
. I'm pretty sure that is an OSGi attribute. The list of supported attributes is defined in the JAR File Specification.Outside frameworks that provide it, there is no built-in support for bundling native libraries inside JAR files. If I recall correctly, it is possible to extract the file to a temporary location and load it manually.
有时我发现问题不是加载本机库的 Java 方式,而是需要本机代码的 3rd 方库。
问题是第 3 方库会在某个时刻(通常在初始化的早期)执行此操作,
并且如果 native.dll 不在适当的位置,则会抛出错误。
如果您有权访问第 3 方库的 java 源代码,则可能很容易修补该代码,并且您可以轻松地从 JAR 中提取 dll 并在使用第 3 方库之前运行 System.load。
更新
我查看了 JNotify 来源。这正是我所说的:
将 *** 行取出或用 try-catch 包围,用 System.load() 加载,然后就完成了。
Sometimes i found the problem is not the Java way of loading native libs, but the 3rd party library that needs that native code.
The problem is that the 3rd party libs will do at some point (normally very early in initialization)
And if native.dll is not at the appropiated place it throws an Error.
If you have access to the java source of the 3rd party library it might be easy to patch that code and you could easily extract your dll from the JAR and run System.load before using the 3rd party lib.
Update
I had a look into JNotify sources. It is exactly what i said:
Take line *** out or surround with try-catch, load with System.load() and you are done.
当程序在该操作系统上运行时尝试挂钩 Windows 关闭事件时,我遇到了这个问题。我最终使用的解决方案本质上是 McDowell 的 - 将 DLL 添加到 jar 文件中,并在程序启动时将其提取到临时位置。如果它适合您的程序,您可以将 DLL 保留在更永久的位置,然后在后续程序启动时引用它。我的应用程序在用户可能故意删除不应该删除的文件的环境中使用,因此我必须在每次运行时提取 DLL。然而,它并没有对性能造成任何重大影响。
I ran into this problem when trying to hook a Windows shutdown event when the program runs on that OS. The solution I ended up using was essentially McDowell's - adding the DLL to the jar file and extracting it to a temporary location when the program starts. If it fits your program, you can leave the DLL in a more permanent place and then reference it on subsequent program startups. My application was used in an environment where the users might intentionally delete files they shouldn't, so I had to extract the DLL on every run. However, it hasn't resulted in any performance hit of significance.