Runtime.exec() fails sometime to execute a command

J

jaideep.barde

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
*********************************/
 
A

Andreas Leitgeb

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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top