Runtime.exec() fails sometime to execute a command

Discussion in 'Java' started by jaideep.barde@gmail.com, Nov 30, 2006.

  1. Guest

    Hello,
    I have a program thats using Runtime.exec to execute some external
    programs sequence with some redirection operators.
    For e.g, I have some command as follows;
    1 - C:\bin\IBRSD.exe IBRSD -s
    2 - C:\bin\mcstat -n @punduk444:5000#mc -l c:\ | grep -i running |
    grep -v grep |wc -l
    3 - ping punduk444 | grep "100%" | wc -l

    ....etc.
    These command in sequence for a single run. The test program makes
    multiple such runs. So my problem is sometimes the runtime.exec() fails
    to execute some of the commands above (typically the 2nd one). The
    waitFor() returns error code (-1). That is if I loop these commands for
    say 30 runs then in some 1~4 runs the 2nd command fails to execute and
    return -1 error code.

    Can some one help me out to as why this is happening? Any help is
    appreciated
    Thanks,
    ~jaideep

    Herer is the code snippet;


    Runtime runtime = Runtime.getRuntime();
    //create process object to handle result
    Process process = null;
    commandToRun = "cmd /c " + command;
    process = runtime.exec( commandToRun );

    CommandOutputReader cmdError = new
    CommandOutputReader(process.getErrorStream());
    CommandOutputReader cmdOutput = new
    CommandOutputReader(process.getInputStream());
    cmdError.start();
    cmdOutput.start();

    CheckProcess chkProcess = new CheckProcess(process);
    chkProcess.start();

    int retValue = process.waitFor();

    if(retValue != 0)
    {
    return -1;
    }
    output = cmdOutput.getOutputData();

    cmdError = null;
    cmdOutput = null;
    chkProcess = null;



    /*******************************supporting CommandOutputReader class
    *********************************/
    public class CommandOutputReader extends Thread
    {
    private transient InputStream inputStream; //to get output of any
    command
    private transient String output; //output will store command output
    protected boolean isDone;

    public CommandOutputReader()
    {
    super();
    output = "";
    this.inputStream = null;
    }

    public CommandOutputReader(InputStream stream)
    {
    super();
    output = "";
    this.inputStream = stream;
    }

    public void setStream(InputStream stream)
    {
    this.inputStream = stream;
    }

    public String getOutputData()
    {
    return output;
    }

    public void run()
    {
    if(inputStream != null)
    {
    final BufferedReader bufferReader = new BufferedReader(new
    InputStreamReader(inputStream), 1024 * 128);
    String line = null;
    try
    {
    while ( (line = bufferReader.readLine()) != null)
    {
    if (ResourceString.getLocale() != null)
    Utility.log(Level.DEBUG,line);
    //output += line +
    System.getProperty(Constants.ALL_NEWLINE_GETPROPERTY_PARAM);
    output += line + "\r\n";
    System.out.println("<< "+ this.getId() + " >>" + output );
    }

    System.out.println("<< "+ this.getId() + " >>" + "closed the i/p
    stream...");
    inputStream.close();
    bufferReader.close();

    }
    catch (IOException objIOException)
    {
    if (ResourceString.getLocale() != null)
    {
    Utility.log(Level.ERROR,
    ResourceString.getString("io_exeception_reading_cmd_output")+
    objIOException.getMessage());
    output =
    ResourceString.getString("io_exeception_reading_cmd_output");
    }
    else
    {
    output = "io exeception reading cmd output";
    }
    }
    finally {
    isDone = true;
    }
    }
    }

    public boolean isDone() {
    return isDone;
    }
    }

    /*******************************supporting CommandOutputReader class
    *********************************/

    /*******************************supporting process controller class
    *********************************/
    public class CheckProcess extends Thread
    {
    private transient Process monitoredProcess;
    private transient boolean continueLoop ;
    private transient long maxWait = Constants.WAIT_PERIOD;

    public CheckProcess(Process monitoredProcess)
    {
    super();
    this.monitoredProcess = monitoredProcess;
    continueLoop =true;
    }

    public void setMaxWait(final long max)
    {
    this.maxWait = max;
    }

    public void stopProcess()
    {
    continueLoop=false;
    }

    public void run()
    {
    //long start1 = java.util.Calendar.getInstance().getTimeInMillis();
    final long start1 = System.currentTimeMillis();

    while (true && continueLoop)
    {
    // after maxWait millis, stops monitoredProcess and return
    if (System.currentTimeMillis() - start1 > maxWait)
    {
    if(monitoredProcess != null)
    {
    monitoredProcess.destroy();
    //available for garbage collection
    // @PMD:REVIEWED:NullAssignment: by jbarde on 9/28/06 7:29 PM
    monitoredProcess = null;
    return;
    }
    }
    try
    {
    sleep(1000);
    }
    catch (InterruptedException e)
    {
    if (ResourceString.getLocale() != null)
    {
    Utility.log(Level.ERROR,
    ResourceString.getString("exception_in_sleep") +
    e.getLocalizedMessage());
    System.out.println(ResourceString.getString("exception_in_sleep")
    + e.getLocalizedMessage());
    }
    else
    {
    System.out.println("Exception in sleep" +
    e.getLocalizedMessage());
    }
    }
    }

    if(monitoredProcess != null)
    {
    monitoredProcess.destroy();
    //available for garbage collection
    // @PMD:REVIEWED:NullAssignment: by jbarde on 9/28/06 7:29 PM
    monitoredProcess = null;
    }

    }
    }
    /*******************************supporting process controller class
    *********************************/
     
    , Nov 30, 2006
    #1
    1. Advertising

  2. <> wrote:
    > I have a program thats using Runtime.exec to execute some external
    > programs sequence with some redirection operators.
    > For e.g, I have some command as follows;


    > 1 - C:\bin\IBRSD.exe IBRSD -s
    > 2 - C:\bin\mcstat -n @punduk444:5000#mc -l c:\ | grep -i running | grep -v grep |wc -l
    > 3 - ping punduk444 | grep "100%" | wc -l


    > commandToRun = "cmd /c " + command;
    > process = runtime.exec( commandToRun );


    If you read the docu for Runtime.exec() (the one with
    plain String argument first), then you see, what cmd will
    get to see, and perhaps it's not what you expected.

    Btw., there are tricks with grep to avoid the extra inverse-grepping
    for "grep" itself, and the separate line-counting, as well:

    ... | grep -ci [r]unning

    The "-c" does the match-line counting, and the square brackets
    around any literal char in the match-word will prevent grep
    from seeing it's own argument in the ps-output.

    PS: If you're using some unix-toolkit on windows, why not also
    a proper shell?
     
    Andreas Leitgeb, Nov 30, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SnVsaWE=?=
    Replies:
    4
    Views:
    777
    =?Utf-8?B?SnVsaWE=?=
    Nov 7, 2004
  2. vizlab
    Replies:
    3
    Views:
    4,276
    Michael Bar-Sinai
    Oct 17, 2007
  3. Hal Vaughan
    Replies:
    11
    Views:
    1,198
    Gordon Beaton
    May 22, 2006
  4. Replies:
    0
    Views:
    444
  5. Lionel
    Replies:
    36
    Views:
    2,235
    Nigel Wade
    Dec 5, 2007
Loading...

Share This Page