为什么我的“if(p.exitValue()!= 0)”代码运行两次?

时间:2011-04-06 05:07:33

标签: java android

当我使用下面的方法执行shell脚本时,我的“if(p.exitValue()!= 0)”代码在成功时运行TWICE ...有谁知道为什么?此外,当shell脚本失败时,else代码会运行一次,然后无论如何都会再次运行成功代码。我究竟做错了什么?

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? 


         }

       }

1 个答案:

答案 0 :(得分:0)

也许你的void exec(String commander)也被召唤了两次。你检查过了吗?