views:

390

answers:

3

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

+2  A: 

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.

jitter
That's a good suggestion, but unfortunately this is a secure computer -- I won't be able to install or run non-approved third-party apps. Is there a way I could accomplish the same thing by instrumenting the code (beyond simply confirming the path property as noted)?
Adam
@jitter Great answer, thank you!
Jack
+2  A: 

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.
Adam
interesting and strange at the same time. why would they do such a strange thing
jitter
A: 

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.

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.

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?

bm212