3

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:

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?

1

2 Answers 2

2

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.

  • To check the Java version MATLAB is using, type in the MATLAB console version -java
  • To check the Java version of the System, type in the command prompt: java -version. If it is another version, change your JAVA_HOME and Path environment variables to point to the right version.
  • To check in Eclipse:
    1. Right click on your project and select Build Path > Configure Build Path
    2. Go to the Libraries Tab
    3. Double click on the JRE System Library and check is the same version as that used by MATLAB. If not locate it in the system.

If 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

Sign up to request clarification or add additional context in comments.

Comments

0

I finally just set the path to the native library as C:\Program\Matlab\bin\win64 directly in Eclipse. Build path, librairies, select the Native library and Edit.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.