Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UnsatisfiedLinkError When Loading a Library from Java in MATLAB

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.

The java ClassLoader throws the UnsatisfiedLinkError when the load is attempted:

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.

I verified java.library.path in two ways. First, I simply checked that value returned on the MATLAB command line:

>> 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:

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.

I welcome any suggestions for things to try if there is no absolute answer.

Platform: Windows XP MATLAB R2009a Java 1.6

like image 610
Adam Holmberg Avatar asked Nov 20 '09 18:11

Adam Holmberg


3 Answers

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.

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.

Now check where it really looks for the dll. Maybe it appends some additional path or similar and thus can't find it.

like image 184
jitter Avatar answered Oct 22 '22 00:10

jitter


Just found this in the MATLAB docs:

Specifying the Search Path for Sun Java Native Method DLLs

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.

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.

Specifying the Java Library Path

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

$matlab/toolbox/local/librarypath.txt

(where $matlab is the MATLAB root directory represented by the MATLAB keyword matlabroot).

You can augment the search path for native method libraries by editing the librarypath.txt file. Follow these guidelines when editing this file:

  • 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.
like image 35
Adam Holmberg Avatar answered Oct 22 '22 00:10

Adam Holmberg


Put the DLL that you try to load using java.lang.System.loadLibrary into the following directory: $matlabroot\sys\java\jre\win??\jre\bin\

like image 23
a_s Avatar answered Oct 22 '22 01:10

a_s