Posted by
Michael Schmid on
Dec 11, 2014; 5:42pm
URL: http://imagej.273.s1.nabble.com/runtime-fails-when-command-has-arguments-tp5010889p5010906.html
Hi Joe,
if the system command has arguments, you have to put them into a separate String[] array, one by one.
The reason for splitting arguments is the possibility to have spaces inside an argument, e.g. a filename (Any quotes or backslashes that you type as escape characters in the shell should not be part of the arguments! When you type in the terminal, the shell would do this for you)
Runtime.getRuntime()exec("/Applications/scilab-5.4.1.app/Contents/MacOs/bin/scilab-cli", new String[] {"-f","sciscript"});
An alternative that should also work:
ProcessBuilder pb = new ProcessBuilder("/Applications/scilab-5.4.1.app/Contents/MacOs/bin/scilab-cli", "-f", "sciscript");
Process p = pb.start();
https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.htmlhttps://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.htmlMichael
________________________________________________________________
On Dec 11, 2014, at 00:08, Joe Vandergracht wrote:
> A few days I asked about running Mac OS X system commands from an ImageJ plugin. I got two suggestions and had some success with Michaels approach below (thanks Michael):
>
> try {
> Process p = Runtime.getRuntime().exec("/path/myexecutable");
> /here you may read your program's stdout from p.getInputStream()
> } catch (Exception e) {
> //handle errors
> }
>
> This works when the command has no arguments. I can verify that the executable runs because that particular executable processes a file and saves it. The action does indeed occur.
>
> I run into problems when my command has arguments. Specifically, now I am trying to run a scilab script. (Scilab is an opensource matlab-like program). When I type the following from a terminal window it works:
>
> /Applications/scilab-5.4.1.app/Contents/MacOs/bin/scilab-cli -f sciscript
>
> In this case the arguments call a script that runs in scilab and then exits. This works fine from the terminal. But when I try to call it using runtime from a plugin as follows,
>
> Process p = Runtime.getRuntime().exec("/Applications/scilab-5.4.1.app/Contents/MacOs/bin/scilab-cli -f sciscript");
>
> I can verify that scilab opens as a process but it does not appear to recognize the arguments because the scilab process opens but the script actions never happens and the process does not exit. I can see the process running when I check the active system processes. Keep in mind that this works from the terminal, it only fails in runtime.
>
> So my first question is whether anyone can spot my mistake. Perhaps it has something to do with the blank spaces between arguments. (I tried zapping gremlins). If nobody has ideas, it would help if I could see what error message might be being sent out as stdout. Michael provides a tip above, but actually accessing that output is proving difficult. More direction would be appreciated if someone has some readymade code using p.getInputStream() already.
>
> thanks,
>
>
> Joe vanderGracht
>
> --
> ImageJ mailing list:
http://imagej.nih.gov/ij/list.html--
ImageJ mailing list:
http://imagej.nih.gov/ij/list.html