I am trying to call Matlab in Java, I followed the steps given by the documentation : https://fr.mathworks.com/help/matlab/matlab_external/setup-environment.html#bvcubp5
And I tried to compile this code :
import com.mathworks.engine.*;
public class javaPassArg{
public static void main(String[] args) throws Exception{
MatlabEngine eng = MatlabEngine.startMatlab();
double[] p = {1.0, -1.0, -6.0};
double[] r = eng.feval("roots", p);
for (double e: r) {
System.out.println(e);
}
eng.close();
}
}
I added the engine.jar to Eclipse as mentioned

And I configured the environment variable of Windows

I am having a well-known error:

The nativemvm library is not found
Here is the detailed error :
Exception in thread "main" java.lang.UnsatisfiedLinkError: no nativemvm in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at com.mathworks.mvm.MvmImpl.loadLibrary(MvmImpl.java:107)
at com.mathworks.mvm.MvmImpl.setJavaEngineMode(MvmImpl.java:202)
at com.mathworks.engine.MatlabEngine.<clinit>(MatlabEngine.java:69)
at javaPassArg.main(javaPassArg.java:5)
But as you can see in my screen my java.library.path is well defined, and the nativemvm.dll does really exist in the folder ! I also defined it directly in Eclipse, but it's not working either.
Does somebody have an idea?
If you are working with Eclipse it is important to remember to restart the eclipse after you have updated your environment variables externally. Just remember to add the MATLAB library location (matlabroot\bin\[arch]) to the path and make sure that you don't have any conflicting versions e.g. you work with 2018b bu previously installed 2017b and both are in your path. This was what caused my MATLAB installation to bring up that error.
On another note you have to make sure that your system, MATLAB and Eclipse are using the same Java version as MATLAB.
version -javajava -version. If it is another version, change your JAVA_HOME and Path environment variables to point to the right version.Build Path > Configure Build PathLibraries TabIf this doesn't work maybe you have a corrupted version of the engine running. A system restart my be the solution.
Useful links: MATLAB: Configure java environment, MATLAB: Build Java engine programs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With