java 将本机 dll 与 jar 捆绑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1846072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bundling native dll with jar
提问by Maciek Sawicki
Possible Duplicate:
How to bundle a native library and a JNI library inside a JAR?
可能的重复:
如何在 JAR 中捆绑本机库和 JNI 库?
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.
我需要在我的 jar 中包含本机库(jnotify,但我认为这无关紧要)。我想用 NetBeans 来做这件事。
I added Bundle-NativeCode: /lib/jnotify.dll; osname=win32to my manifest.mffile and added jnotify.dllto projektHome\src\lib\folder. But unfortunately NetBeans is overidning manifest.mffile.
我添加Bundle-NativeCode: /lib/jnotify.dll; osname=win32到我的manifest.mf文件并添加jnotify.dll到projektHome\src\lib\文件夹。但不幸的是,NetBeans 正在覆盖 manifest.mf文件。
How can I fixed? Can I do this using only NetBeans? Is it line 'Bundle-NativeCode: /lib/jnotify.dll; osname=win32correct? I also heard I should put dlls hash in manifest.mfand sign my jar. Is that true?
我该如何修复?我可以只使用 NetBeans 来做到这一点吗?行'Bundle-NativeCode: /lib/jnotify.dll; osname=win32正确吗?我还听说我应该放入 dll 哈希manifest.mf并在我的 jar中签名。真的吗?
回答by McDowell
I don't think the Java executable supports Bundle-NativeCode. I'm pretty sure that is an OSGiattribute. The list of supported attributes is defined in the JAR File Specification.
我认为 Java 可执行文件不支持Bundle-NativeCode. 我很确定这是一个OSGi属性。支持的属性列表在JAR 文件规范中定义。
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.
在提供它的外部框架中,没有对在 JAR 文件中捆绑本机库的内置支持。如果我没记错的话,可以将文件解压缩到临时位置并手动加载。
回答by PeterMmm
Sometimes i found the problem is not the Java way of loading native libs, but the 3rd party library that needs that native code.
有时我发现问题不是加载本机库的 Java 方式,而是需要该本机代码的第 3 方库。
The problem is that the 3rd party libs will do at some point (normally very early in initialization)
问题是第 3 方库会在某个时候做(通常在初始化很早)
System.loadLibrary("native.dll");
And if native.dll is not at the appropiated place it throws an Error.
如果 native.dll 不在合适的位置,则会引发错误。
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.
如果您可以访问 3rd 方库的 java 源代码,则可能很容易修补该代码,并且您可以轻松地从 JAR 中提取 dll 并在使用 3rd 方库之前运行 System.load。
UpdateI had a look into JNotify sources. It is exactly what i said:
更新我查看了 JNotify 来源。这正是我所说的:
public class JNotify_win32
{
static
{
System.loadLibrary("jnotify"); /* *** */
int res = nativeInit();
if (res != 0)
{
throw new RuntimeException("Error initialiing native library. (#" + res + ")");
}
}
Take line *** out or surround with try-catch, load with System.load() and you are done.
取出 line *** 或用 try-catch 包围,用 System.load() 加载,就完成了。
回答by spork
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.
当程序在该操作系统上运行时,我尝试挂钩 Windows 关闭事件时遇到了这个问题。我最终使用的解决方案基本上是 McDowell 的 - 将 DLL 添加到 jar 文件并在程序启动时将其解压缩到一个临时位置。如果它适合您的程序,您可以将 DLL 留在更永久的位置,然后在后续程序启动时引用它。我的应用程序在用户可能有意删除他们不应该删除的文件的环境中使用,因此我必须在每次运行时提取 DLL。然而,它并没有导致任何显着的性能下降。

