making system call from a plugin

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

making system call from a plugin

jvander
Short version of question:   Can I call a system level command from a plug in.

Long version:  I have compiled executables from my own C code that run from the command line in a Mac OS X terminal or Xterminal window.  For example an executable might be called myexecutable.  I can run that program by opening a terminal window and typing

./myexecutable

at the prompt, provided I have first navigated to the correct directory.  Not sure why the period and forward slash are needed in front of the executable name, but that's what works in OS X.

I would like to have an ImageJ plugin open up a terminal window, go to the correct directory and execute  the program.

Is this feasible?  If so, any tips or resources would be appreciated.  I am not expert in Java by any means so pointing to examples would be greatly appreciated.

thanks

Joe vanderGracht

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

Re: making system call from a plugin

Michael Schmid
Hi Joe,

search the Web for "Runtime.getRuntime().exec" (on Google with the quotes), and you will find many examples.
On OS X and Linux, you can directly use the String as you type it in the terminal, on Windows you will have to prepend "cmd /c " to the command String.

If you want to specify a default directory (path) to be used by the program, the three-argument form is best:

try {
  Process p = Runtime.getRuntime().exec("/home/me/bin/myexecutable", //command
                null, //may be a String[] with environmental parameters,
                myPathDirectory);
  /here you may read your program's stdout from p.getInputStream()
} catch (Exception e) {
  //handle errors
}

By the way, also the ImageJ macro language can do this, see the exec macro command.

Michael
________________________________________________________________
On Dec 4, 2014, at 17:24, Joe Vandergracht wrote:

> Short version of question:   Can I call a system level command from a plug in.
>
> Long version:  I have compiled executables from my own C code that run from the command line in a Mac OS X terminal or Xterminal window.  For example an executable might be called myexecutable.  I can run that program by opening a terminal window and typing
>
> ./myexecutable
>
> at the prompt, provided I have first navigated to the correct directory.  Not sure why the period and forward slash are needed in front of the executable name, but that's what works in OS X.
>
> I would like to have an ImageJ plugin open up a terminal window, go to the correct directory and execute  the program.
>
> Is this feasible?  If so, any tips or resources would be appreciated.  I am not expert in Java by any means so pointing to examples would be greatly appreciated.
>
> 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: making system call from a plugin

Peter Haub
In reply to this post by jvander
Hi Joe,

you can try something like this:

import ij.plugin.PlugIn;
public class Run_MyCommand implements PlugIn{
     public void run(String arg) {
         try {
             List<String> comStr = new ArrayList<String>();
             comStr.add("nameOfExecutable");
             comStr.add("Parameter1");
             comStr.add("Parameter2");
             comStr.add("Parameter3");
             // etc.

             ProcessBuilder pb = new  ProcessBuilder(comStr);
             Process p = pb.start();

             p.waitFor();
             System.out.println("Done.");
             p.destroy();
             p = null;
             pb = null;
         } catch (Exception e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
     }
}

regards
peter

On 04.12.2014 17:24, Joe Vandergracht wrote:

> Short version of question:   Can I call a system level command from a plug in.
>
> Long version:  I have compiled executables from my own C code that run from the command line in a Mac OS X terminal or Xterminal window.  For example an executable might be called myexecutable.  I can run that program by opening a terminal window and typing
>
> ./myexecutable
>
> at the prompt, provided I have first navigated to the correct directory.  Not sure why the period and forward slash are needed in front of the executable name, but that's what works in OS X.
>
> I would like to have an ImageJ plugin open up a terminal window, go to the correct directory and execute  the program.
>
> Is this feasible?  If so, any tips or resources would be appreciated.  I am not expert in Java by any means so pointing to examples would be greatly appreciated.
>
> thanks
>
> Joe vanderGracht
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

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