Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
failure to run hidapi, java.lang.UnsatisfiedLinkError

Subject: failure to run hidapi, java.lang.UnsatisfiedLinkError

From: Roozbeh

Date: 4 Feb, 2012 23:24:35

Message: 1 of 4

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)
Does anyone know how to solve this problem. Below are what I have done so far to explore/solve the problem:
- The jnilib file is already added to Matlab path (librarypath.txt). It is not the case that Matlab does not see the jnilib file. My other java classes that rely on other jni files in the same directory run successfully in Matlab.
- The problem happens with both 32 bit and 64 bit compilations of the jnilib file on both 32 bit and 64 bit Matlab. A mismatch between the jni library and Matlab is ruled out.
- Because my java code runs successfully outside Matlab the code in the HIDAPI library can not be at fault. The jnilib file and its related C/C++ functions are kosher.
- My latest hypothesis is that Matlab may be lacking some of the dynamic libraries that are called by the C/C++ functions. Running 'otool -L' on the jnilib produces the following report:
/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit (compatibility version 1.0.0, current version 275.0.0)
/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 550.44.0)
/usr/lib/libiconv.2.dylib (compatibility version 7.0.0, current version 7.0.0)
/usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 7.9.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.11)
I am not sure how to make Matlab see all of these libraries. I have added their paths to librarypath.txt but UnsatisfiedLinkError persists.
Any hypotheses, ideas or solutions? I will greatly appreciate any help.

Subject: failure to run hidapi, java.lang.UnsatisfiedLinkError

From: Yair Altman

Date: 5 Feb, 2012 08:43:10

Message: 2 of 4

"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

Subject: failure to run hidapi, java.lang.UnsatisfiedLinkError

From: Roozbeh Kiani

Date: 5 Feb, 2012 09:13:10

Message: 3 of 4

Thanks Yair. The path to the jni file is already added to librarypath.txt (and is present in LD_LIBRARY_PATH). Also, I have already implemented LoadLibrary, following one of your earlier posts. I can soundly confirm that I can load the jni file in Matlab without invoking UnsatisfiedLinkError. The error seems to happen when I call the functions of the jni file. Because I can call those functions outside Matlab without invoking an error I assume that UnsatisfiedLinkError is caused because Matlab fails to load the dependencies of the jni file (i.e., the dynamic libraries it calls). How can I confirm it? Does Matlab load all dependencies of the jni file when it is loaded? I assume that Matlab loads them only when the related functions in the jni file are called. How can I ensure Matlab loads those files?
-roozbeh

Subject: failure to run hidapi, java.lang.UnsatisfiedLinkError

From: Roozbeh Kiani

Date: 5 Feb, 2012 10:17:09

Message: 4 of 4

The problem is solved by moving the java class that calls the functions of the jni library to the static java path of Matlab. Apparently, Matlab had a problem with calling the jni functions from the dynamic java path. I do not understand this oddity of Matlab but I am happy to report that the problem is solved.
 

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us