0

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?

D. Sill
  • 277
  • 1
  • 9
  • "I've also used a tcsh shell to add /bin/maci64 to the DYLD_LIBRARY_PATH variable." Where did you do that? I'm guessing this does not affect applications you run from the OS launcher (such as Eclipse & Netbeans), it will only work if you open them from your Terminal. There are solutions you can find, depending on your OSX version, to set such variables at the session level (so it affects all apps) – Hugues M. May 13 '17 at 08:34
  • I found a guide online saying how to go into preferences on terminal, change the "shell opens with" to "bin/tcsh" and launch a new terminal window. Then use "setenv DYLD_LIBRARY_PATH ...". Is this not the correct way to set this variable? I'm new to this so not sure if this is correct. Thanks – D. Sill May 13 '17 at 08:38

1 Answers1

0

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).

Hugues M.
  • 19,846
  • 6
  • 37
  • 65
  • Thanks for your reply. I think setting it in the app would be fine for now. Where might i find the launch configuration? I've got both netbeans and eclipse so either is fine. – D. Sill May 13 '17 at 11:16
  • From memory with Eclipse: when you run your app, you click on a button. On the side of this button there is a dropdown. In the dropdown you should get a "Launch Configuration..." item. – Hugues M. May 13 '17 at 11:17
  • Ok i found this and added the variable there, but I'm still getting the same error. Not sure if there's something else I'm missing here. – D. Sill May 13 '17 at 11:28
  • In your `main` method, before anything, try to print the value of `System.getProperty("java.library.path")`, see if you have the correct directories, and that they have the expected native library in there. – Hugues M. May 13 '17 at 11:31
  • I get the following.. /Users/Darren/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:. – D. Sill May 13 '17 at 11:44
  • This is missing `/bin/maci64` (your initial question suggests this is the path you need). If you correctly set up the Launch Config, then you should get this entry printed. Are you positive there is no typo or mistake? It should look like [this screenshot](http://help.eclipse.org/mars/topic/org.eclipse.ptp.doc.user/html/images/rms-ibmpe5-env.png) except it's a Java entry and variable name is `DYLD_LIBRARY_PATH` – Hugues M. May 13 '17 at 12:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/144129/discussion-between-hugues-moreau-and-d-sill). – Hugues M. May 13 '17 at 14:00