runtime fails when command has arguments

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

runtime fails when command has arguments

jvander
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
Reply | Threaded
Open this post in threaded view
|

ROI selected in the ROI Manager

lartaud marc
Hi,
Since the 1.49k version
when I want to modify un ROI selected in the ROI Manager
for example by adding (Shift-clicking) a selection with the rectangular
selection Tool
The ROI is deseleted in the ROI Manager
so I can't make update
I have to add another ROI and delete the old!
I think this is due to :
"Thanks to Philippe Carl, an ROI selected in the ROI Manager is
deselected when it is removed from the image"

Regards,
Marc



--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: ROI selected in the ROI Manager

CARL Philippe (LBP)
Dear Marc,
I'm really sorry that the update I had requested has modified the way you were using some of the features of the ROI Manager.
Actually I had as well already seen the issue you are describing together with another one.
Namelly in the case you want to select several ROIs within the ROI Manager (select the first one, than Shift and select the second one,...) only the first one is shown up within the picture which I guess isn't completely right and could maybe be updated in the same time than your request.
My best regards,
Philippe

Philippe CARL
Laboratoire de Biophotonique et Pharmacologie
UMR 7213 CNRS - Université de Strasbourg
Faculté de Pharmacie
74 route du Rhin
67401 ILLKIRCH
Tel : +33(0)3 68 85 41 84

-----Message d'origine-----
De : ImageJ Interest Group [mailto:[hidden email]] De la part de lartaud marc
Envoyé : jeudi 11 décembre 2014 09:16
À : [hidden email]
Objet : ROI selected in the ROI Manager

Hi,
Since the 1.49k version
when I want to modify un ROI selected in the ROI Manager for example by adding (Shift-clicking) a selection with the rectangular selection Tool The ROI is deseleted in the ROI Manager so I can't make update I have to add another ROI and delete the old!
I think this is due to :
"Thanks to Philippe Carl, an ROI selected in the ROI Manager is deselected when it is removed from the image"

Regards,
Marc



--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: runtime fails when command has arguments

Michael Schmid
In reply to this post by jvander
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.html
https://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html

Michael
________________________________________________________________
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
Reply | Threaded
Open this post in threaded view
|

Re: runtime fails when command has arguments

jvander
In reply to this post by jvander
Thanks again Michael.  It turned out that my actual problem was a different one.  I had not specified the path for the sciscript.   Once I did this the original method worked:

Process p = Runtime.getRuntime().exec("/commandpath/scilab-cli -f /scriptpath/sciscript");

So having the -f with white space followed by the script did not actually cause my problem.  Before I figured the path problem out, I did study the documentation pages on Runtime that you provided and I did indeed like the command array approach.  I ended up modifying your proposal as follows:

String cmdarray[]={"/commandpath/scilab-cli",  "-f",   "/scriptpath/sciscript"};
Process p = Runtime.getRuntime().exec(cmdarray);

This is a more modular approach so thank you very much for your help.  

On a more general note, the marriage of ImageJ and the command line interpreter for scilab is a huge boon for me.  I have used ImageJ for visual manipulation and to perform a wide range of standard postprocessing for years, but my lack of skills in java programming has prevented me from writing more serious code.  Through the use of runtime I can use the power and simple command environment of a program like scilab or matlab  to implement sophisticated algorithms (in my case iterative Fourier transform processes).   ImageJ then serves as a sort of "super GUI" that calls the scilab program but also manipulates the output.  This is a big step forward for me.   ImageJ is a fantastic resource and Wayne and the community are great in helping folk like me with limited programming experience.  I am thankful indeed.

Of course this hybrid approach is not the ideal, but given my limited time to spend learning java this is a great solution for me.

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: ROI selected in the ROI Manager

Rasband, Wayne (NIH/NIMH) [E]
In reply to this post by lartaud marc
On Dec 11, 2014, at 3:15 AM, lartaud marc <[hidden email]> wrote:
>
> Hi,
> Since the 1.49k version
> when I want to modify un ROI selected in the ROI Manager
> for example by adding (Shift-clicking) a selection with the rectangular selection Tool
> The ROI is deseleted in the ROI Manager
> so I can't make update
> I have to add another ROI and delete the old!

This regression is fixed in the latest ImageJ daily build (1.49n10).

-wayne

> I think this is due to :
> "Thanks to Philippe Carl, an ROI selected in the ROI Manager is deselected when it is removed from the image"
>
> Regards,
> Marc

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html