Windows上的Java JNI和依赖库 [英] Java JNI and dependent libraries on Windows

查看:358
本文介绍了Windows上的Java JNI和依赖库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

长话短说:我有一个可执行jar,调用 jni.dll ,它依赖于 lib.dll 。而且我得到了那么可怕的 UnsatisfiedLinkError

Long story short: I have an executable jar, that calls jni.dll which is dependent on lib.dll. And I'm getting the oh-so-dreaded UnsatisfiedLinkError.

这个答案非常接近,但根据我的经验,它无法解决问题。即使在 java.library.path 中指定了dll驻留的文件夹,它也不起作用。我也必须更改Windows PATH 环境变量。实际上,Windows上的默认 java.library.path 似乎是 PATH

This answer comes pretty close, yet it fails to solve the problem, from my experience. Even when the folder where the dll's reside is specified in java.library.path, it will not work. I have to change the Windows PATH environment variable as well. In fact, the default java.library.path on Windows seems to be PATH.

有没有漂亮的方法来解决这个问题?我想为Windows构建一个安装程序,我想知道如何处理这个问题,以便最终用户不必进行任何手动工作。

Is there any "pretty" way to fix this? I want to build an installer for Windows and I'm wondering how I would deal with this issue, so that the end-user will not have to do any manual work.

编辑:

我实现的内容如下:应用程序附带一个名为native_libs的文件夹,该文件夹具有适用于所有受支持体系结构的动态库。结构如下:

What I implemented is the following: the application ships with a folder called "native_libs" which has dynamic libraries for all supported architectures. The structure is the following:

/
+- native_libs/
   +- windows/
   |  +- x86/
   |  |  +- ...
   |  +- x64/
   |     +- ...
   |
   +- linux/
   |  +- x86/
   |  |  +- ...
   |  +- x64/
   |     +- ...
   |
   +- libs/
      +- ...

在运行时,应用程序初始化,检测到正确的JRE体系结构和系统OS,并将正确的库文件复制到libs /文件夹。 java.library.path 正在运行时使用常见的hack进行设置。最后,使用原生启动器设置Windows的 PATH 环境变量。

On runtime, while the application initializes, the correct JRE architecture and System OS are detected and the proper library files are copied to the libs/ folder. The java.library.path is being set on runtime as well using a common hack. Finally, the PATH environment variable for windows is set using a native launcher.

还有改进空间吗?也许将dll复制到与 jar 文件相同的目录中将无需设置 java.library.path PATH 变量?我需要研究用 System.load()加载dll,这将否定复制文件的需要。

Any room for improvement? Maybe copying the dll's in the same directory as the jar file would negate the need for setting the java.library.path and PATH variables? I need to investigate loading the dll's with System.load() as well, which will negate the need to copy files.

推荐答案

java.library.path 指定 System.loadLibrary()查找动态库文件。如果在代码中更改 java.library.path 系统属性,则不会产生任何影响。有些黑客可以让Java忘记初始值并重新评估 java.library.path 系统属性的内容。

java.library.path specifies the directories where System.loadLibrary() looks for the dynamic library file. If you change the java.library.path system property in your code, it will not have any effect. There are hacks to make Java "forget" the initial value and re-evaluate the contents of the java.library.path system property.

但是,依赖库不是由Java加载的,它是由Windows加载的。 Windows不关心 java.library.path ,它只关心 PATH 环境变量。您唯一的选择是为您的Java进程调整 PATH 。例如,如果从批处理文件启动它,请在java调用之前更改 PATH 环境变量。

However, the dependent library is not loaded by Java, it's loaded by Windows. Windows does not care about java.library.path, it only cares about the PATH environment variable. Your only option is to adjust PATH for your Java process. For example, if you start it from a batch file, change the PATH environment variable right before the java invocation.

这篇关于Windows上的Java JNI和依赖库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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