如何使用Eclipse在.JAR文件中包含特定于平台的本机库? [英] How can I include platform-specific native libraries in the .JAR file using Eclipse?

查看:128
本文介绍了如何使用Eclipse在.JAR文件中包含特定于平台的本机库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始学习JNI。我一直在关注一个简​​单的例子,我创建了一个Java应用程序,它在本机库中调用Hello World方法。我想针对Win32和Linux x86。

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.

我的库驻留在DLL中,当DLL被添加到我的Eclipse项目的根目录时,我可以使用LoadLibrary调用它。

但是,我无法弄清楚如何让Eclipse导出包含DLL和Linux的.SO文件的可运行JAR。

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.

所以我的问题基本上是;您将如何在Eclipse中创建项目并包含相同本机库的多个版本?

So my question is basically; how would you go about creating a project in Eclipse and include several versions of the same native library?

谢谢,

Martin

Thank you,
Martin

推荐答案

对于可运行的JAR,您需要做的是提取到临时目录(可能在静态{}块中)然后加载DLL从该目录(在同一块中使用System.loadLibrary())。为此,您需要继承ClassLoader并覆盖findLibrary()方法以允许在该目录中找到库。您可以在此处执行所需的任何逻辑来加载特定平台库。说实话,不同平台上的库命名应该是相似的 - 我相信你在加载时省略了'lib'部分和扩展名。这就是它的要点。可能更容易使用One-JAR,因为另一张海报提到:)

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

这篇关于如何使用Eclipse在.JAR文件中包含特定于平台的本机库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆