Runtime r = Runtime.getRuntime();
Process p = r.exec("cmd.exe /c" + " start  .\\batch.bat");
try {
    p.waitFor();
    System.out.println("Run to here");
    System.out.println("Run to here 2222222");
} catch (InterruptedException e) {
    e.printStackTrace();
}
发现batch.bat还在跑,"Run to here"已经打出来了,
JavaDoc里说waitFor()方法会等到该子进程结束。
求教,调用外部shell时怎么同步

解决方案 »

  1.   

    解决了。呵呵
    try {
    Runtime r = Runtime.getRuntime(); Process proc = r.exec("cmd.exe /c" + " start  .\\backup.bat");

    try {
    InputStream stderr = proc.getErrorStream();
                InputStreamReader isr = new InputStreamReader(stderr);
                BufferedReader br = new BufferedReader(isr);
                String line = null;
                System.out.println("<ERROR>");
                while ((line = br.readLine()) != null)
                    System.out.println(line);
                System.out.println("</ERROR>");
                int exitVal = proc.waitFor();
                System.out.println("ProcessexitValue: " + exitVal);
                this.batchFile.delete(); } catch (InterruptedException e) {
    e.printStackTrace();
    }
    } catch (IOException e) {
    e.printStackTrace();
    }