Hey there!
I am currently trying to get Java code running in Matlab using a native library. My problem is not the general setup, but a single specific dll file that constantly causes UnsatisfiedLinkError exceptions. I am not able to load this specific dll file from within Java (I am trying to connect a software called SUMO to Matlab). So to give you more details, see this example code in java:
import org.eclipse.sumo.libsumo.*;
public class LibsumoTest {
public static void main (String[] args){
System.out.println("Let's go!");
try{
System.out.println("Loading dll: " + args[0] + "\\libsumojni.dll");
System.load(args[0] + "\\libsumojni.dll");
System.out.println("Success!");
// Do stuff with native library
System.out.println("Version: " + libsumo.getCMD_GETVERSION());
}
catch(UnsatisfiedLinkError ex){
System.out.println(ex.getLocalizedMessage());
System.out.println(ex.getMessage());
ex.printStackTrace();
}
}
}
Running this on (windows) command line interface using the provided jar file:
cd C:\test
javac -cp "libsumojni.jar;C:\test" LibsumoTest.java
java -cp "libsumojni.jar;C:\test" LibsumoTest "C:\test"
the output is:
Let's go!
Loading dll: C:\test\libsumojni.dll
Success!
Version: 0
As you can see, the native library is working in this context (of course I tested way more complicated examples, this is just a minimal example).
Now I want to do the same in Matlab, but if I run this code in matlab:
cd C:\test
javaaddpath(pwd);
javaaddpath([pwd '\libsumojni.jar']);
LibsumoTest.main(pwd);
I always get this error:
Let's go!
Loading dll: C:\test\libsumojni.dll
C:\test\libsumojni.dll: Die angegebene Prozedur wurde nicht gefunden
C:\test\libsumojni.dll: Die angegebene Prozedur wurde nicht gefunden
java.lang.UnsatisfiedLinkError: C:\test\libsumojni.dll: Die angegebene Prozedur wurde nicht gefunden
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1941)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1824)
at java.lang.Runtime.load0(Runtime.java:809)
at java.lang.System.load(System.java:1086)
at LibsumoTest.main(LibsumoTest.java:12)
I am using Matlab 2019b (64 bit), my jdk is 64 bit, the dll file is 64 bit. I also tried to use the static classpath instead of the dynamic one with no change. As long as I take another dll file, there is no error while loading. I made so many tests with different dll files which all worked that I come to the conclusion that there must be something special with this dll that prevents Matlab from loading it. Here is a screenshot of dependency walker with the loaded dll:
Even the function names are correct regarding the jni naming convention. However, there seem to be some problems (red font) but I am not really familiar with them. Do you have an idea where the problem can come from and why in the case using java without matlab this is working? I am also confused by MSVCP140.dll and VCRUNTIME140.dll which are displayed as 32 bit instead of 64 bit?
Any ideas?
Thank you very much
Sven