MATLAB Answers

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

6 views (last 30 days)
Nina on 31 Jan 2011
Edited: Yair Altman on 30 Dec 2016


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:
	at mylib.mylibJNI.new_myClass__SWIG_0(Native
	at mylib.myClass.<init>(
Error in ==> getDelta at 23

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

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


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 Comment
Evgenii Nekhoroshev
Evgenii Nekhoroshev on 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.

Sign in to comment.

Answers (3)

Siddharth Shankar
Siddharth Shankar on 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 on 3 Feb 2011
Edited: Yair Altman on 30 Dec 2016
I have seen a report that adding dynamic libraries cannot be done directly from Matlab because of an issue with Matlab's classloader:
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)
You would then load your library in Matlab as follows:
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 on 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.
edit classpath.txt
To automatically open the correct file without having to search for it.

Community Treasure Hunt

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

Start Hunting!

Translated by