windows 如何使用 Eclipse 在 .JAR 文件中包含特定于平台的本机库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2702132/
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
How can I include platform-specific native libraries in the .JAR file using Eclipse?
提问by Martin Wiboe
I am just starting to learn JNI. I have been following a simple example, and I have created a Java app that calls a Hello World method in a native library. I'd like to target Win32 and Linux x86.
我刚刚开始学习JNI。我一直在关注一个简单的例子,我创建了一个 Java 应用程序,它在本机库中调用 Hello World 方法。我想以 Win32 和 Linux x86 为目标。
My library resides in a DLL, and I can call it just fine using LoadLibrary when the DLL is added to the root of my Eclipse project.
However, I can't figure out how to get Eclipse to export a runnable JAR that includes the DLL and the .SO file for Linux.
我的库驻留在一个 DLL 中,当 DLL 添加到我的 Eclipse 项目的根目录时,我可以使用 LoadLibrary 很好地调用它。
但是,我不知道如何让 Eclipse 导出一个可运行的 JAR,其中包含适用于 Linux 的 DLL 和 .SO 文件。
So my question is basically; how would you go about creating a project in Eclipse and include several versions of the same native library?
所以我的问题基本上是;您将如何在 Eclipse 中创建一个项目并包含同一个本机库的多个版本?
Thank you,
Martin
谢谢你,
马丁
采纳答案by Chris Dennett
For runnable JARs, what you need to do is extract to the temporary directory (maybe in a static { } block) and then load the DLL from that directory (using System.loadLibrary() in the same block). To do this, you need to subclass ClassLoader and override the findLibrary() method to allow libraries to be found in that directory. You can do whatever logic you need to here to load the particular platform libraries. To be honest, the naming for the libraries on the different platforms should be similar anyway -- I believe that you omit the 'lib' part when loading, and the extension. That's the gist of it. Probably easier to use One-JAR as the other poster mentioned :)
对于可运行的 JAR,您需要做的是提取到临时目录(可能在静态 { } 块中),然后从该目录加载 DLL(在同一块中使用 System.loadLibrary())。为此,您需要继承 ClassLoader 并覆盖 findLibrary() 方法以允许在该目录中找到库。您可以在此处执行所需的任何逻辑来加载特定平台库。老实说,无论如何,不同平台上库的命名应该是相似的——我相信你在加载时省略了“lib”部分和扩展名。这就是它的要点。可能更容易使用 One-JAR 作为另一张海报提到:)