你的运行的命令写错了或者命令运行的时候有很多的输出  你没有得到  阻塞了import java.util.*;
import java.io.*;/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */public class Untitled1 {
  public Untitled1() {
  }
  public static void main(String[] args) throws Exception {
    Untitled1 untitled11 = new Untitled1();
    System.out.println(Untitled1.runCommand("cmd /c dir"));
  }
  static public runCommand(String cmd) throws IOException {
java.lang.Process proc = Runtime.getRuntime().exec(cmd);
                  InputStream istr = proc.getInputStream();
                  BufferedReader br =
                  new BufferedReader(new InputStreamReader(istr));
                  String str;
                  while ((str = br.readLine()) != null)
System.out.println(str);                  try {
                          int i = proc.waitFor();
                          System.out.println(i);
                  }
                  catch (InterruptedException e) {
                          System.err.println("process was interrupted");
                  }
                   if (proc.exitValue() != 0)
                          System.err.println("exit value was non-zero");                  // close stream
                  br.close();
          }}

解决方案 »

  1.   

    上面的程序有点问题import java.util.*;
    import java.io.*;/**
     * <p>Title: </p>
     * <p>Description: </p>
     * <p>Copyright: Copyright (c) 2003</p>
     * <p>Company: </p>
     * @author not attributable
     * @version 1.0
     */public class Untitled1 {
      public Untitled1() {
      }  public static void main(String[] args) throws Exception {
        Untitled1 untitled11 = new Untitled1();
        Untitled1.runCommand("cmd /c dir");
      }  static public void runCommand(String cmd) throws IOException {
        java.lang.Process proc = Runtime.getRuntime().exec(cmd);
        InputStream istr = proc.getInputStream();
        BufferedReader br =
            new BufferedReader(new InputStreamReader(istr));
        String str;
        while ( (str = br.readLine()) != null)
          System.out.println(str);    try {
          int i = proc.waitFor();
          System.out.println(i);
        }
        catch (InterruptedException e) {
          System.err.println("process was interrupted");
        }
        if (proc.exitValue() != 0)
          System.err.println("exit value was non-zero");      // close stream
        br.close();
      }}
    这个是正确的
      

  2.   

    估计
     p = r.exec("latex 1.tex", null, new File("c:/"));
    java程序在等待本地进程运行结束.
      

  3.   

    上面问题已经解决!通过读取process的InputStream流,当in.read()返回-1时,判断子进程结束。前提是调用的命令要有返回信息,于是新的问题产生:如果没有返回值怎么办?
    jokerjava:
    如你所写:
    ************************
        try {
          int i = proc.waitFor();
          System.out.println(i);
        }
        catch (InterruptedException e) {
          System.err.println("process was interrupted");
        }
        if (proc.exitValue() != 0)
          System.err.println("exit value was non-zero");
    *************************
    i一直等于4,我想要的是,判断(进程是否结束)没有:什么也不做;结束:程序继续。
      

  4.   

    我这里 一直是0啊
    如果不是0
    会打印exit value was non-zero
      

  5.   

    int i = proc.waitFor();
    i会得到该进程运行结束后的返回值
    一般正常结束会返回0,非正常结束返回非0值,
    返回值是用类似java中System.exit(n)语句
    或C/C++中exit(0)语句返回的