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

5 visualizaciones (últimos 30 días)
Nina
Nina el 31 de En. de 2011
Editada: Yair Altman el 30 de Dic. de 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 comentario
Evgenii Nekhoroshev
Evgenii Nekhoroshev el 28 de Oct. de 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.

Iniciar sesión para comentar.

Respuestas (3)

Siddharth Shankar
Siddharth Shankar el 31 de En. de 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 el 3 de Feb. de 2011
Editada: Yair Altman el 30 de Dic. de 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 el 31 de En. de 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.

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by