|
"Roozbeh " <r_kiani@yahoo.com> wrote in message <jgkeni$k9n$1@newscl01ah.mathworks.com>...
> I have a problem with using a JNI wrapper in Matlab. The wrapper is from the HIDAPI library (http://code.google.com/p/javahidapi/), which most of you may not be familiar with, but I think the problem roots in how Matlab handles JNI files. So, please read on if you have any experience with using JNI in Matlab. I can successfully compile the C/C++ code of HIDAPI and create the dynamic library. Also, I can load the library in my java code and run the java code successfully from a *terminal* (under OSX). However, when I try to use the same java code in *Matlab* (2009b and 2011b) I get this error:
> ??? Java exception occurred:
> java.lang.UnsatisfiedLinkError: com.codeminders.hidapi.HIDManager.init()V
> at com.codeminders.hidapi.HIDManager.init(Native Method)
> at com.codeminders.hidapi.HIDManager.<init>(HIDManager.java:65)
> at com.codeminders.hidapi.HIDManagerTest.<init>(HIDManagerTest.java:19)
[snip]
An UnsatisfiedLinkError exception is thrown if JNI encounters any error when loading a library file. This could range from file-not-found, to not being on the librarypath, to not being loadable as a dynamic library, and so on. It is therefore always prudent to test for this exception when loading native libraries.
Admittedly, all this is far from trivial. It gets even more complicated when we try to access Matlab native (dynamic library) functionality using JNI: The basic building block of JNI usage is the java.lang.System.loadLibrary(libName) method. Unfortunately, unlike almost any other Java method that can be tested from the Matlab Command Prompt, it appears that some internal bug or limitation in Matlab’s classloader prevents direct usage of System.loadLibrary from the Command Prompt. Instead, it can only be used from within Java code (i.e., a user-created Java class).
Therefore, to test our dynamic library, we need to create a simple Java class that does the actual loadLibrary, and then call that class from Matlab:
public class LoadLibrary
{
public static void loadLibrary(String s)
{
try {
System.loadLibrary(s);
} catch (UnsatisfiedLinkError error) {
System.out.println("Error loading library "+s);
}
}
}
We have several alternatives of specifying the librarypath in Matlab: we can set the LD_LIBRARY_PATH environment variable, or add a corresponding -Djava.library.path= directive to our java.opts file.
Alternatively, we could add a line in the librarypath.txt file, which is located in the %matlabroot%/toolbox/local/ folder. Type edit('librarypath.txt') at the Matlab Command Prompt to edit this file in the Matlab Editor, or use any external text editor for this. If we do not have administrator access to this file, then we can also place a copy of this file in our user's Matlab startup folder.
Once we have librarypath.txt set-up correctly and have restarted Matlab, we can now load our library in Matlab as follows:
javaaddpath('path-to-the-folder-that-contains-LoadLibrary.class');
LoadLibrary.loadLibrary('libMylib.so'); % or libMylib.dll in Windows
Using JNI in Matlab is described in detail in Section 9.5 of my Matlab-Java Programming book: http://UndocumentedMatlab.com/matlab-java-book/
Yair Altman
http://UndocumentedMatlab.com
|