【问题标题】:JNA finds library running in Eclipse but not when a built JAR fileJNA 找到在 Eclipse 中运行的库,但在构建 JAR 文件时找不到
【发布时间】:2015-11-16 12:34:16
【问题描述】:

我正在编写一个需要使用 .dll 文件的程序。当我的程序启动时,会执行以下代码:

public static void main(String[] args)
{
    String dLLURL = "C:/Program Files (x86)/Location of DLL";
    System.setProperty("jna.library.path", dLLURL);
    System.setProperty("jna.debug_load", "true");
    System.setProperty("jna.debug_load.jna", "true");
    Application.launch(args);
}

然后我执行 DllInterface dllinterface = (DllInterface) Native.loadLibrary( "dllName.dll", DllInterface.class);

...加载正确的 dll 并允许我使用它。 JNA 输出以下 (这次使用 DLL 的实际名称/路径)

Trying (via loadLibrary) jnidispatch
Looking in classpath from sun.misc.Launcher$AppClassLoader@e2f2a for /com/sun/jna/win32-x86/jnidispatch.dll
Found library resource at jar:file:/C:/Users/bengs_000/Downloads/jna.jar!/com/sun/jna/win32-x86/jnidispatch.dll
Trying C:\Users\BENGS_~1\AppData\Local\Temp\jna-792348840\jna2314341730536889248.dll
Found jnidispatch at C:\Users\BENGS_~1\AppData\Local\Temp\jna-792348840\jna2314341730536889248.dll
Looking for library 'RailDriver.dll'
Adding paths from jna.library.path: C:\Program Files (x86)\Steam\SteamApps\common\RailWorks\plugins
Trying C:\Program Files (x86)\Steam\SteamApps\common\RailWorks\plugins\RailDriver.dll
Found library 'RailDriver.dll' at C:\Program Files (x86)\Steam\SteamApps\common\RailWorks\plugins\RailDriver.dll

这就是我在 Eclipse IDE 中运行程序时发生的情况。效果很好!

但是,当我将程序作为 .jar 构建/运行时,应用程序说找不到 DLL(尽管在正确的位置查找)并返回以下消息:

Trying (via loadLibrary) jnidispatch
Looking in classpath from java.net.URLClassLoader@677327b6 for /com/sun/jna/win32-x86-64/jnidispatch.dll
Found library resource at jar:rsrc:jna.jar!/com/sun/jna/win32-x86-64/jnidispatch.dll
Trying C:\Users\BENGS_~1\AppData\Local\Temp\jna-792348840\jna8530559464473818762.dll
Found jnidispatch at C:\Users\BENGS_~1\AppData\Local\Temp\jna-792348840\jna8530559464473818762.dll
Looking for library 'RailDriver.dll'
Adding paths from jna.library.path: C:\Program Files (x86)\Steam\SteamApps\common\RailWorks\plugins
Trying C:\Program Files (x86)\Steam\SteamApps\common\RailWorks\plugins\RailDriver.dll
Adding system paths: []
Trying C:\Program Files (x86)\Steam\SteamApps\common\RailWorks\plugins\RailDriver.dll
Looking for lib- prefix
Trying libRailDriver.dll
Looking in classpath from java.net.URLClassLoader@677327b6 for RailDriver.dll
There was an error finding your raildriver.dll file

如您所见,查找 .dll 文件的路径完全相同,但它在 Eclipse 中工作,而不是作为已编译的 .jar 文件!

您能帮我找出问题吗?需要注意的一点是 .dll 文件存储在 Program Files 的 32 位部分中,而我的计算机是 64 位的。我正在使用 Java 32bit 在 Eclipse 中编译和运行程序。当然它应该用 Java 32bit 构建 .jar 对吗?

【问题讨论】:

  • 我想我需要以某种方式强制我的应用程序以 32 位模式执行(从 .jar),因为 DLL 是 32 位我的电脑是 64 位,但它在 Eclipse 中以 32 位运行。

标签: eclipse dll build jna 32-bit


【解决方案1】:

我找到了解决办法!

背景

我知道问题与我想使用的 .dll 为 32 位,而我的计算机为 64 位有关。在 Eclipse IDE 中,我使用 Java 32 位运行应用程序(我知道我应该这样做以确保兼容性)。

运行 .jar 文件时,Java 默认为 64 位版本;与我在 Eclipse 中测试它的方式不同。

解决方案

要强制用户使用 Java 32 位,您需要将 .jar 捆绑到 .exe 文件中。我使用 Launch4J 来做到这一点。 Launch4j 中有一个参数允许您指定要使用的 Java 系统。它位于“搜索选项”部分的 JRE 选项卡下。通过将其更改为“仅 32 位”,.jar 文件在 Java 32 位下启动,从而缓解了问题!

我希望这可能对某人有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-17
    • 2012-01-04
    • 1970-01-01
    • 1970-01-01
    • 2015-07-10
    • 2020-05-16
    相关资源
    最近更新 更多