1
当我使用下面的方法执行shell脚本时,我的“if(p.exitValue()!= 0)”代码在成功时运行TWICE ...任何人都知道为什么?另外,当shell脚本失败时,else代码会运行一次,然后再次运行成功代码。我究竟做错了什么?为什么我的“if(p.exitValue()!= 0)”代码运行两次?
void exec(String commander){
Process p = null;
try {
p = Runtime.getRuntime().exec(commander);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
StreamGobbler errorGobbler = new
StreamGobbler(p.getErrorStream(), "ERROR");
// any output?
StreamGobbler outputGobbler = new
StreamGobbler(p.getInputStream(), "OUTPUT");
// kick them off
errorGobbler.start();
outputGobbler.start();
// any error???
int exitVal = 1;
try {
exitVal = p.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("ExitValue: " + exitVal);
if (p.exitValue() != 0)
{
//SUCCESS Code RUNS TWICE
}
else {
//FAILURE Code Runs Once, then Success Code Runs anyway!! WHY?
}
}
在这段代码中,我看不到任何内容,显示为什么该行会运行两次。你能发布调用exec()的代码吗? – jwatts1980 2011-04-06 05:11:32
为了更快得到更好的帮助,请发布[SSCCE](http://pscode.org/sscce.html)。 – 2011-04-06 12:02:49