How can I suppress MATLAB command window when calling it from Java?
Have a look at Matlab Engine . The engine runs in the background (no GUI or visible command line) and you call it from your code. Unfortunately the examples are in C and Fortran, not Java. I got it working with Python once, but I don't remember the details.
Also see: 2 Ways to Use the Engine with Java .
ETA: "matlab -r" instruction in command line (Windows) will execute "instruction" in Matlab. My Python hack is putting my Matlab code in a .m file and my data in a text file referenced by the .m file and then sending "matlab -r myFile.m" to the windows command line. See the matlab Windows command . Again, there is no visible GUI for Matlab this way.
source to share
When you say "calling it from Java", are you bypassing the Matlab command for batch computing, or do you want to insert a long running Matlab session into your process and call M code from Java code multiple times? What OS do you want to run?
Matlab has several deployment tools that allow you to embed a Matlab interpreter and a set of Matlab sources inside a host language such as C / C ++ or Java. This is that the "Matlab compiler" is not a real compiler, but a tool that packages the Matlab runtime along with the .m source code in a package that looks like a DLL or an application. "Standalone Matlab Application" is Matlab code that has been packaged this way along with a thin C wrapper that calls the application entry point into your M code.
Matlab Java Builder is the same thing that bundles this expanded Matlab engine inside a Java class. If you want to get a license for this, it can make it easier and cosmetically to clean up the embedded Matlab inside your Java application. This is probably what you want.
These deployed Matlab applications do not have command windows as they are meant to be shared with your application. They live in the same process. And last but not least, they do not require licensing fees to run a deployed application. Deploying regular Matlab requires all users to run it to obtain a license for Matlab and every toolkit used.
If you remove the shell, the command line "matlab -nosplash -nodesktop" will suppress the GUI on Unix. But on Windows, you still get a minimal Matlab command window. The "-automation" switch on Windows will at least minimize it. I don't know how to completely suppress it at startup.
However, once Matlab is up and running, you can take advantage of the fact that the Matlab GUI itself is implemented in Java and hide it. Get your Matlab session to run this hidematlab () with the command line "-r" or startup.m. Note that this is a hack using undocumented internals of Matlab and is clearly not supported by MathWorks.
function hidematlab()
%HIDEMATLAB Hide the main Matlab desktop window (HACK)
dtWin = desktopwindow();
if ~isempty(dtWin)
dtWin.setVisible(0);
end
function out = desktopwindow()
%DESKTOPWINDOW Find the main Matlab desktop window (HACK)
wins = java.awt.Window.getOwnerlessWindows();
out = [];
for i = 1:numel(wins)
if isa(wins(i), 'com.mathworks.mde.desk.MLMainFrame')
out = wins(i);
return;
end
end
Beware of gotchas when bombarded on Windows where Matlab is inherently a graphical application. If your M code throws errors that appear at the top level or a segfault, you may find that your Matlab session was hanging waiting for non-existent user input, instead of returning you an error.
source to share
I do not know how to do what you are asking completely. If your script exits manually instead of naturally terminating, you can run the script to keep the window that pops up to a minimum.
Cm. Running application minimized from Java
Run your script with
matlab -nojvm -nosplash -nodesktop -wait -r script_name
You need "-wait", otherwise MATLAB will return immediately.
See How can I stop MATLAB returning before the command line script finishes?
source to share
matlabcontrol is a Java API that will allow you to interact with the current MATLAB session. It will start a session and then you can use eval and feval and set and get variables. By default, a MATLAB session will be visible, but can be hidden . On Windows, this will not be completely hidden because it is not supported by MATLAB, but it will run with the minimum value and no splash screen will appear. To get started using matlabcontrol take a look at walkthrough .
source to share