windows 在 MATLAB 中从 Java 加载库时出现 UnsatisfiedLinkError
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1772572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UnsatisfiedLinkError When Loading a Library from Java in MATLAB
提问by Adam Holmberg
I've been integrating simple java modules into the MATLAB environment on Windows with some success. Recently I encountered a problem with a third-party library which attempts to load a dll.
我一直在将简单的 java 模块集成到 Windows 上的 MATLAB 环境中,并取得了一些成功。最近我遇到了一个尝试加载dll的第三方库的问题。
The java ClassLoader throws the UnsatisfiedLinkError when the load is attempted:
尝试加载时,java ClassLoader 会抛出 UnsatisfiedLinkError:
java.lang.UnsatisfiedLinkError: no <libname> in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
The exception is reporting that my 'libname' is not in the java.library.path property. I have verified that the property does indeed have the correct path in it, and that the libname.dll file exists on that path.
例外是报告我的“libname”不在 java.library.path 属性中。我已经验证该属性确实包含正确的路径,并且 libname.dll 文件存在于该路径中。
I verified java.library.path in two ways. First, I simply checked that value returned on the MATLAB command line:
我通过两种方式验证了 java.library.path。首先,我只是检查了 MATLAB 命令行上返回的值:
>> java.lang.System.getProperty('java.library.path')
Then, I modified the java method in question to print that value just before the call into the failing third-party function:
然后,我修改了有问题的 java 方法以在调用失败的第三方函数之前打印该值:
System.out.println(System.getProperty('java.library.path'));
Both confirmed that my path value was set as expected.
两者都确认我的路径值已按预期设置。
I've also tried manually loading the library from the command line, and it fails with the same results.
我还尝试从命令行手动加载库,但失败并得到相同的结果。
Is this something that is not possible in MATLAB, or am I missing something here? Unfortunately I'm not administrator on this machine so I can't try the old trick of moving the dll into a directory with dlls that do work.
这在 MATLAB 中是不可能的,还是我在这里遗漏了什么?不幸的是,我不是这台机器的管理员,所以我不能尝试将 dll 移动到一个目录中的老技巧,其中 dll 可以工作。
I welcome any suggestions for things to try if there is no absolute answer.
如果没有绝对的答案,我欢迎任何尝试的建议。
Platform: Windows XP MATLAB R2009a Java 1.6
平台:Windows XP MATLAB R2009a Java 1.6
采纳答案by Adam Holmberg
Just found this in the MATLAB docs:
刚刚在 MATLAB 文档中找到了这个:
Specifying the Search Path for Sun Java Native Method DLLs
指定 Sun Java Native Method DLL 的搜索路径
The mechanism that MATLAB uses to locate native method libraries that are required by Java has changed. MATLAB no longer uses system environment variables to define the paths to these libraries.
MATLAB 用于定位 Java 所需的本机方法库的机制已更改。MATLAB 不再使用系统环境变量来定义这些库的路径。
Compatibility Considerations
兼容性注意事项
If you presently rely on the PATH (for Windows) or LD_LIBRARY_PATH (for UNIX) environment variables for this purpose, you will need to use the file librarypath.txt, as described below, in its place.
如果您目前为此依赖 PATH(对于 Windows)或 LD_LIBRARY_PATH(对于 UNIX)环境变量,您将需要使用文件 librarypath.txt,如下所述,代替它。
Specifying the Java Library Path
指定 Java 库路径
Java classes can dynamically load native methods using the Java method java.lang.System.loadLibrary("LibFile"). In order for the JVM software to locate the specified library file, the directory containing it must be on the Java Library Path. This path is established when MATLAB launches the JVM software at startup, and is based on the contents of the file
Java 类可以使用 Java 方法 java.lang.System.loadLibrary("LibFile") 动态加载本机方法。为了让 JVM 软件找到指定的库文件,包含它的目录必须在 Java 库路径上。该路径是MATLAB在启动时启动JVM软件时建立的,基于文件的内容
$matlab/toolbox/local/librarypath.txt
$matlab/toolbox/local/librarypath.txt
(where $matlab is the MATLAB root directory represented by the MATLAB keyword matlabroot).
(其中 $matlab 是由 MATLAB 关键字 matlabroot 表示的 MATLAB 根目录)。
You can augment the search path for native method libraries by editing the librarypath.txt file. Follow these guidelines when editing this file:
您可以通过编辑 librarypath.txt 文件来增加本地方法库的搜索路径。编辑此文件时请遵循以下准则:
- Specify each new directory on a line by itself.
- Specify only the directory names, not the names of the DLL files. The LoadLibrary call does this for you.
- To simplify the specification of directories in cross-platform environments, you can use any of these macros: $matlabroot, $arch, and $jre_home.
- 在一行中单独指定每个新目录。
- 仅指定目录名称,而不是 DLL 文件的名称。LoadLibrary 调用会为您执行此操作。
- 为了简化跨平台环境中的目录规范,您可以使用以下任何宏:$matlabroot、$arch 和 $jre_home。
回答by jitter
Are you familiar with Process Monitor? (If not you will easily get how it works).
你熟悉进程监视器吗?(如果不是,您将很容易了解它是如何工作的)。
Download it. Run it. Just enable "Show File System Activity" (little icons on right under menu-bar), disable the others.
下载它。运行。只需启用“显示文件系统活动”(菜单栏右侧的小图标),禁用其他。
Then fire up whatever causes the library to try to load the dll. After the UnsatisfiedLinkError
occured, stop the event capturing in Process Monitor.
然后启动导致库尝试加载 dll 的任何内容。在之后UnsatisfiedLinkError
发生,阻止事件进程监视器捕获。
Now do a CTRL+F and search for the name of the dll it should load. Check the (probably multiple) entry which say "Not Found" or "Name not found" in the result column and with the dll-name in the path column.
现在执行 CTRL+F 并搜索它应该加载的 dll 的名称。检查结果列中显示“未找到”或“未找到名称”的(可能是多个)条目以及路径列中的 dll 名称。
Now check where it really looks for the dll. Maybe it appends some additional path or similar and thus can't find it.
现在检查它真正寻找 dll 的位置。也许它附加了一些额外的路径或类似的路径,因此找不到它。
回答by a_s
Put the DLL that you try to load using java.lang.System.loadLibrary into the following directory: $matlabroot\sys\java\jre\win??\jre\bin\
将您尝试使用 java.lang.System.loadLibrary 加载的 DLL 放入以下目录:$matlabroot\sys\java\jre\win??\jre\bin\
回答by bm212
Does your library depend on other dlls? It could be that the dll java is loading as a result of its loadLibrary() call requires other dlls. On Windows, I believe it will look on %PATH% to try to find these dlls.
您的库是否依赖于其他 dll?可能是 dll java 正在加载,因为它的 loadLibrary() 调用需要其他 dll。在 Windows 上,我相信它会在 %PATH% 上寻找这些 dll。
This isn't strictly a java thing; it's more to do with the native library you are loading. Java is told where to find the dll via java.library.path (or whatever other mechanism Matlab uses), and the libname.dll will use %PATH% to find any dlls it depends on.
这不是严格意义上的 Java 东西;它更多地与您正在加载的本机库有关。通过 java.library.path(或 Matlab 使用的任何其他机制)告诉 Java 在哪里可以找到 dll,并且 libname.dll 将使用 %PATH% 来查找它所依赖的任何 dll。
As you say that the missing dll is in your java.library.path, perhaps you could try appending the java.library.path value to %PATH% and trying again?
正如您所说,缺少的 dll 在您的 java.library.path 中,也许您可以尝试将 java.library.path 值附加到 %PATH% 并重试?