Search code examples
javamacosmatlabmatlab-engine

Issue using mathworks engine API for java on Mac


I'm trying to use the Mathworks MATLAB Engine API for Java on my Macbook Pro.

By following this guide on the mathworks website I have added the /extern/engines/java/jar/engine.jar to the classpath, and I've also used a tcsh shell to add /bin/maci64 to the DYLD_LIBRARY_PATH variable.

However when I attempt to run the simple code below, I get the error message;

Exception in thread "main" java.lang.UnsatisfiedLinkError: no nativemvm in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1864)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1122)
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 MatlabTest.main(MatlabTest.java:7)

I have tried this in both Netbeans and Eclipse and I get the same error. This is the code I'm trying to run:

import com.mathworks.engine.*;

public class MatlabTest {

public static void main(String[] args) throws Exception {

MatlabEngine mtlb = MatlabEngine.startMatlab();

double[] vals = {2.0, 3.0, 5.0};

double[] ans = mtlb.feval("javaTest", vals);

for (double n: ans){
System.out.println(n);
}

mtlb.close();

}

}

Has anyone experience the same thing or knows what I might be doing wrong?


Solution

  • There are several "solutions", depending on what you want to achieve.

    The guide you followed allows you to change the environment variables for the shells that Terminal executes. Therefore those changes only affect processes (and apps) started from your Terminal.

    Solution 1: you only need this environment variable when running the app. So, instead of setting the environment in the Terminal, you can set it in Eclipse or Netbeans in the launch configuration of your program (in Eclipse, there is an Environment tab in the Launch Configuration settings).

    Solution 2 (assuming your Terminal does have the correct env with DYLD_LIBRARY_PATH following your changes) : start your IDE from the Terminal: close Eclipse, and open it from your terminal using command open -a Eclipse (or open -a "Eclipse blah blah", you need the exact name of the application, with quotes if it contains spaces)

    When opened this way, Eclipse should "see" your variable.

    For a more persistent solution, you need to search for how to set environment variables at session level (or for all users), and the solution depends on which version of the OS you are running (please specify that and maybe we can help direct you to relevant links).