try{
   Runtime.getRuntime().exec("dir").waitFor();}catch(Exception e){}

解决方案 »

  1.   

    外部exe程序的返回结果可以用Process.exitValue()取得。调用Process.exitValue()时,如果外部exe程序还在运行,则会抛出例外。所以在Java中要用Process.waitFor()来等待外部exe程序的结束。例:
    public class StatusCode
    {
        public static void main(String argv[])
        {
            Runtime runtime = Runtime.getRuntime();
            try{
                Process process = runtime.exec("test.exe");            process.waitFor();
                System.out.println("Exit Code:" + process.exitValue());
            } catch(Exception e){
                e.printStackTrace();
            }
        }
    }
      

  2.   

    感谢各位, 我也试过,但程序无法向下进行。一直停在waitFor()那
    Runtime r=Runtime.getRuntime(); 
    Process p=null; //rar是winrar的exe程序
    String cmd[]={"d:\\temp\\rar","x","d:\\temp\\1.zip","d:\\temp\\"}; 
    try{ 
      p=r.exec(cmd); 
      p.waitFor();
      System.out.println(p.exitValue());
    } catch(Exception e){ 
      System.out.println(e); 
    }
      

  3.   

    public void exe(String zt_ip)
        {
    String dmlink="";
           try {
           Process process = Runtime.getRuntime().exec(zt_ip);
                 }
            catch (Exception e){
                e.printStackTrace();
                }
        return ;
      } for (int i=0; i<1; i++)
          exe("notepad");
      }
      

  4.   

    另起一个Thread调用waitFor() 
      

  5.   

    laolin(老林) 的是对的,我测试过了(我本来以前自己没考虑过,现在]学习)你的代码有问题,改String cmd[]={"d:\\temp\\rar","x","d:\\temp\\1.zip","d:\\temp\\"}; 
    为String cmd="d:/tem/rar x d;/temp/1.zip d:/temp/"(不知rar是否这样传参数)。