Running Matlab from java ProcessBuilder, Matlab console not showing up on Mac OS 10.8

This is a really weird app, so bear with me. The primary application consists of a Matlab database with a user interface written in Java Swing. To start the application, follow these steps from the command line:

matlab -r "initMatlab;runJavaUI;" 

Suppose that initMatlab and runJavaUI are both Matlab functions in the matlab runtime path generated by this command. This basically spawns a matlab instance, then runs these two matlab functions immediately after starting Matlab. Strange, this command line option does not work on all operating systems. In addition, not all versions of Matlab are compatible with all versions of Java (our client wants to use REALLY old Matlab installations ... cannot force them to change).

So ... we provide a Java-executable Jar that automatically generates command-line arguments based on the operating system you are running on: the user simply double-clicks it to display a small user interface, and then several options are presented. I will name this jar Launcher . This Launcher detects all installed instances of Java and Matlab and allows the user to choose which combination of Java and Matlab will be used. The jar uses ProcessBuilder to run the matlab command, which works fine, except for one thing. On Windows, Launcher launches Matlab just fine, we see that the Matlab console is displayed, then the Java interface looks as it should. However, on a Mac running Mountain Lion (10.8, I reckon), the Matlab console never appears - this does not mean that Matlab is not working, although I can still make Matlab calls from the Java interface. This does not prevent the user from using the user interface, but sometimes Matlab will display error messages to the console, which is problematic because on the Mac the user will never know when something goes wrong. The strangest thing is that I tried to print the created command line, which does not start the Matlab console when starting through ProcessBuilder , but does ProcessBuilder Matlab console when starting through the terminal. Once again, Matlab does not start, it does not allow you to make your own user interface visible.

I tried to copy all Launcher environment variables to ProcessBuilder before starting Matlab, but to no avail. So I have to leave this a little open, but tried to start Matlab with ProcessBuilder , and if so, what did you do to get the Matlab console to appear on Mac OS Mountain Lion?

Sscce

Export this code into a jar executable, and then run Mac OS Mountain Lion:

 java -jar launch-matlab.jar /absolute/path/to/matlab 

You should see the Matlab icon on the dock, but you cannot make the Matlab console window visible. If you run this on Windows, the Matlab console will appear as it should.

 public class LaunchMatlab { public static void main(String[] args) throws Exception{ String matlabExe = "matlab"; if(args != null && args.length > 0) matlabExe = args[0]; ProcessBuilder pb = new ProcessBuilder(); pb.command(matlabExe,"-wait"); pb.environment().putAll(System.getenv()); System.err.println("Launching Matlab using following PB args: "+ pb.command()); Process p = pb.start(); System.err.println("Waiting for Matlab to exit ..."); p.waitFor(); System.err.println("Matlab exited, launcher exiting ..."); } } 
+4
source share
1 answer

You need to specify that MATLAB should start with a visible user interface using the -desktop flag. I do not believe this is documented.

So you need to execute the command

 matlab -desktop -r "initMatlab;runJavaUI;" 

I created a Java library called matlabcontrol that can abstract all of this from you. It can run MATLAB while running on Windows, OS X, and Linux, and then allows you to interact with MATLAB through the eval and feval commands. The matlabcontrol code to run MATLAB is in matlabcontrol.RemoteMatlabProxyFactory createProcess(...) . If you use matlabcontrol as a Java library, you will not directly interact with this class or this method.

+4
source

Source: https://habr.com/ru/post/1493009/


All Articles