calling Java methods using JNI/C++ (via Swig) from Matlab -> UnsatisfiesLinkError

5 vues (au cours des 30 derniers jours)
Nina
Nina le 31 Jan 2011
Modifié(e) : Yair Altman le 30 Déc 2016

Hi,

I have got the following problem:

I would like to call a Java program from Matlab. The Java program calls a C++ procedure via JNI/SWIG. Calling the Java method without the Matlab framework, everything works fine. But when calling the Java method from Matlab, I get an UnsatisfiedLinkError.

??? Error using ==> javaObject
Java exception occurred:
java.lang.UnsatisfiedLinkError:
mylib.mylibJNI.new_myClass__SWIG_0()J
	at mylib.mylibJNI.new_myClass__SWIG_0(Native
      Method)
	at mylib.myClass.<init>(myClass.java:39)
Error in ==> getDelta at 23
javaObject('mylib.myClass')

In Java, I defined a class 'myClass.java' in the package 'mylib'. With SWIG, the JNI interface class 'mylibJNI.java' and the library 'libMylib.so' are created.

In Matlab, I then load the classes and the library:

javaObject('mylib.myClass')
javaObject('mylib.mylibJNI')
java.lang.System.load('libMylib.so')
java.lang.System.load('<path>/libMylib.so')

But then, I get the error above.

Additionally, I added the path of the library to the variables $PATH and $LD_LIBRARY_PATH$, but I still get the same error.

I tried many things, but I have not any ideas any more. Could somebody give me a hint, why calling libraries does not work from Matlab, although it works when calling them directly from Java?

Thanks in advance!

  1 commentaire
Evgenii Nekhoroshev
Evgenii Nekhoroshev le 28 Oct 2016
I am not sure, but the reason could be that you are trying to use a 32 bit library with a 64 bit Matlab. By default, Matlab runs a bundled JVM which is different from that you are using during development. 64 bit Matlab requires 64 bit JVM and it won't be able to run 32 bit dll's. Changing the default JVM in Matlab to 32bit (using MATLAB_JAVA environment variable) does not help as it won't start. Try to install 32 bit Matlab.

Connectez-vous pour commenter.

Réponses (3)

Siddharth Shankar
Siddharth Shankar le 31 Jan 2011
Please try adding the path to the C++ library inside the file: librarypath.txt
1. From the MATLAB command prompt:
edit librarypath.txt
2. Add the path to your C++ library, save and close. 3. Restart MATLAB, try running your Java program from within MATLAB now.

Yair Altman
Yair Altman le 3 Fév 2011
Modifié(e) : Yair Altman le 30 Déc 2016
I have seen a report that adding dynamic libraries cannot be done directly from Matlab because of an issue with Matlab's classloader: https://www.kitware.com/InfovisWiki/index.php/Matlab_Titan_Toolbox#Overcome_Matlab_loadLibrary_bug
The solution is to create a simple Java class that does the actual library load, and then call that class from Matlab:
class LoadLibrary
{
public static void loadLibrary(String s)
{
java.lang.System.loadLibrary(s);
}
}
You would then load your library in Matlab as follows:
javaaddpath('path-to-the-folder-of-LoadLibrary')
LoadLibrary.loadLibrary('libMylib.so')
Note that the report mentioned above references an official Matlab bug report, that for some unknown reason has since been removed: 1-1A2HO.
Perhaps this reported bug has been fixed in past releases and perhaps not - I am not sure of its current state.
Yair Altman

Martijn
Martijn le 31 Jan 2011
Please make sure you add both your JAR-file and the directory containing the native library to MATLAB's static Java Path. So do not use JAVAADDPATH, instead add the directory and file to classpath.txt.
Hint:
Type
edit classpath.txt
To automatically open the correct file without having to search for it.

Catégories

En savoir plus sur Startup and Shutdown dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by