必须将所有依赖的DLL放入JDK的bin文件夹中? [英] Must I place all dependent DLLs into the JDK's bin folder?

查看:1164
本文介绍了必须将所有依赖的DLL放入JDK的bin文件夹中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的java应用程序依赖于一个DLL,该DLL 进一步取决于 libstdc ++ - 6.dll



我试图:




  • 放置 libstdc ++ -6.dll 在一个文件夹中

  • ,并将文件夹放在%PATH%



然后我遇到 java.lang.Unsatisfied LinkError:从Eclipse启动应用程序时,无法找到指定的过程



但是,如果将 libstdc ++ - 6.dll 放入 JDK的bin文件夹 ,说 C:\Java\jdk1.6.0_45_32bit\bin 。它的工作正常。



但是我不想污染JDK文件夹。我记得Windows会搜索%PATH%来找到相关的DLL。为什么我不能在这个问题上使用%PATH%?



更新1



Windows中的%PATH%环境变量。




  • 用户变量

  • 系统变量



我只是意外发现:




  • 如果我将DLL的文件夹放到User%PATH%,找不到。


  • 如果我把DLL的文件夹放到System%PATH%,它可以工作。 p>




为什么?



更新2



受此线程的启发:系统与用户PATH环境变量... winmerge只有当我添加路径到用户PATH



我开始想知道我的用户%Path%是否太长。所以我将包含我的依赖DLL的文件夹路径从User%PATH%的结束移动到开始。它现在工作!



起初,我得出结论,一个实现Windows的DLL查找算法的人有一些截断问题。而且我几乎认为它是另一个恼人的Windows Bug。



但是我写了另一个具有相似的DLL依赖关系的Windows应用程序,以确认我的猜测。该应用程序工作正常!所以我必须回顾一下我的结论。



我逐一检查了我的User%PATH%条目,并将文件夹放在每个可能的位置。最后,我发现根本原因


我有一个 C:\\用户%PATH%中的\\MinGW\bin 条目,其中包含
libstdc ++ - 6.dll(977KB)不幸的是,这不兼容
与我需要的$ code>(825KB)。它只有在我将文件夹放在MinGW之前才有效。因此,在%PATH%解决方案中实际上是DLL冲突。


现在这个问题似乎解决了。但是另一个出现,如果我想要使用我的DLL和MinGW,我需要来回切换?



更新3



请通过@AndyThomas查看评论。他提到为直接和间接DLL使用 System.loadLibrary()。这样,我们需要关心的就是 java.library.path 属性。我认为这是一个一劳永逸的解决方案。

解决方案

首先:将所需的DLL文件放在同一目录下

然后:加载本机库 - 这样做有3个选项:


  1. 设置运行应用程序时的VM选项。




-Djava.library.path =C:\您的目录,其中Dll是
存在


示例:


java -Djava.library.path =C:\您的Dll是
目录-jar app.jar





  1. 从应用程序内加载特定的本地库:




a)将包含文件aaa.dll的目录直接放在Java项目下。



b)放置此行在您应用程序堆栈跟踪的顶部: System.loadLibrary(aaa)





  1. 在您的应用程序中使用VM选项:




System.setProperty(java.library.path,C:\您的Dll是
目录);



My java application depends on a DLL, and that DLL further depends on libstdc++-6.dll.

I tried to:

  • placed the libstdc++-6.dll in a folder
  • and put the folder in the %PATH%

Then I meet the java.lang.Unsatisfied LinkError: The specified procedure could not be found when launching application from Eclipse.

But if I put the libstdc++-6.dll into the JDK's bin folder, say C:\Java\jdk1.6.0_45_32bit\bin. It works fine.

But I don't want to pollute the JDK folder. I remember windows will search %PATH% to locate dependent DLLs. Why can't I use the %PATH% in this issue?

Update 1

There are 2 different %PATH% environment variables in Windows.

  • User variables
  • System variables

I just accidentally find that:

  • If I put the DLL's folder to User %PATH%, it cannot be found.

  • If I put the DLL's folder to System %PATH%, it works.

Why?

Update 2

Inspired by this thread:System versus user PATH environmental variable...winmerge works only if I add the path to the user PATH

I start to wonder maybe my User %Path% is too long. So I moved the folder path containing my dependent DLL from the end of User %PATH% to the beginning. It works now!

At first, I conclude that one who implemented the Windows' DLL lookup algorithm has some truncation issue. And I almost consider it as another annoying Windows Bug.

But I wrote another Windows application which has similar DLL dependencies to confirm my guess. That application works fine! So I have to review my conclusion.

I checked my User %PATH% entry one by one, and place the folder to each possible location. And finally, I find the root cause.

I have a C:\MinGW\bin entry in User %PATH%, which happens to contain a libstdc++-6.dll (977KB) but unfortunately, which isn't compatible with the one I need (825KB). It only works if I place my folder before MinGW. So it's actually DLL collision during %PATH% resolution.

Now this issue seems resolved. But another one comes up, do I need to switch back and forth if I want to use both my DLL and the MinGW?

Update 3

Please check the comment by @AndyThomas. He mentioned using System.loadLibrary() for both direct and indirect DLLs. This way, all we need to care about is the java.library.path property. I think that's a once-for-all solution.

解决方案

First: put all DLL files you need in the same directory

Then: Load native libs - to do so you have 3 options:

  1. Set VM Options while you run your app.

-Djava.library.path="C:\Your Directory where Dll is present"

Example:

java -Djava.library.path="C:\Your Directory where Dll is present" -jar app.jar

  1. Load specific native library from within the app:

a) Place the directory that contains the file aaa.dll directly under the Java project.

b) And place this line on the top of stack trace of your app: System.loadLibrary("aaa")

  1. Use VM options from within your app:

System.setProperty( "java.library.path", "C:\Your Directory where Dll is present" );

这篇关于必须将所有依赖的DLL放入JDK的bin文件夹中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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