gpt4 book ai didi

带有重新分发的 JRE 的 Java : cannot find native dependencies

转载 作者:太空宇宙 更新时间:2023-11-04 06:25:52 24 4
gpt4 key购买 nike

我想用我的应用程序重新分发 jre。我在使用 native 库时遇到问题。

假设我有一个 jar:application.jar,它依赖于两个 native 库 (dll):a.dll 和 b.dll。全部都在同一目录中。

Application.jar 包含一个类:Test.java 包 com.mytest;

public class Test {
static {
System.loadLibrary("a");
System.loadLibrary("b");
}

public static void main(String[] args) {
System.out.println("hooray");
}
}

执行时:java -jar application.jar 它工作得很好。

然后我将 JRE 文件夹从程序文件复制到以下文件夹:C:\data\jre\

现在我执行:c:\data\jre\bin\java.exe -jar application.jar 它失败并显示以下消息:

Exception in thread "main" java.lang.UnsatisfiedLinkError: C:\data\application\b.dll: The specified procedure could not be found
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary1(ClassLoader.java:1965)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1890)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1880)
at java.lang.Runtime.loadLibrary0(Runtime.java:849)
at java.lang.System.loadLibrary(System.java:1088)
at com.mytest.Test.<clinit>(Test.java:6)

当我删除加载库 b 的行时,它工作得很好。既然库ab在同一个目录下,为什么系统找不到b

最佳答案

解决方案是b.dll依赖于其他库。其中一个库也放置在 jre bin 目录中,但不存在于 C:\windows\system32\ 中。

当使用普通的java命令运行时,它会首先查看system32文件夹,但在其中找不到dll。然后它查找放置a.dllb.dll的文件夹。

当使用c:\data\jre\bin\java.exe命令运行时,它会首先在c:\data\jre\bin\the.dll中查找dll,然后再查找a.dllb.dll所在的文件夹。

不允许重新分发没有此 dll 的 JRE。因此,解决方法是在加载 b.dll 之前使用 System.load("C:/full/path/to/dll/the.dll") 显式加载正确的 dll。

这是一次艰难的搜索,但关键是使用 procmon并比较两个执行情况。

关于带有重新分发的 JRE 的 Java : cannot find native dependencies,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26801272/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com