这样:
public class Test1 { public static void main(String[] args) {
try {
Runtime run=Runtime.getRuntime();
Process process=null;

//必须要分清楚那些是命令(exe),哪些是程序(bat)
//process=run.exec("java");
//process=run.exec("javac");
process=run.exec("cmd.exe /c ant");
int exitValue=-1;
//<1>
//process.exitValue()将不会等待Runtime.exec()运行完毕便将运行,故得到一个异常
//exitValue=process.exitValue();
//</1>

//<2>
//process.waitFor()将无限等待下去,不会退出
//exitValue=process.waitFor();
//</2>

//<3>
InputStream errorIn=process.getErrorStream();
InputStreamReader streamReader=new InputStreamReader(errorIn);
BufferedReader bufferedReader=new BufferedReader(streamReader);
InputStream inIn=process.getInputStream();
Read readInput=new Read(inIn);
Read readError=new Read(errorIn);
Thread threadError=new Thread(readError);
Thread threadIn=new Thread(readInput);
threadIn.start();
threadError.start();
exitValue=process.waitFor();
//</3>

System.out.println("value="+exitValue);
}
catch (Exception e) {
e.printStackTrace();
}
}
}class Read implements Runnable{
private InputStream in=null;
InputStreamReader streamReader=null;
BufferedReader bufferedReader=null;
public void run(){
try {
streamReader=new InputStreamReader(in);
bufferedReader=new BufferedReader(streamReader);
String line="";
while((line=bufferedReader.readLine())!=null){
System.out.println(line);
}
}
catch (IOException e) {
e.printStackTrace();
}
finally{
try{
streamReader.close();
bufferedReader.close();
in.close();
}
catch(Exception  e){}
}
}
public Read(InputStream in){
this.in=in;
}
}

解决方案 »

  1.   

    谢谢,我以前也是这样的 ,但是没有你这么详细
    我的程序是 
         String[] command=new String[];
                      command[0]="java";
                      command[1]="B";
            Process child = Runtime.getRuntime().exec(command);
    这样不行,然后我该成       
                     String[] command=“r.bat”;
            Process child = Runtime.getRuntime().exec(command); 还是不行,现在试试你的吧,希望可以。谢谢!!
      

  2.   

    ...那是你写得不对,单说语法,应该是:
    <<
    String[] command = new String[]{"java", "B"};

    String[] command = new String[]{"r.bat"};
    >>
      

  3.   

    上面是我打错了,其实我的意思是  String[] command=new String[2]; 和你的意思是一样的。
    2楼的程序我测试过了,OK  :). 那我还想问,我该怎样结束它(B),因为我的A是一个定时启动B的程序,在启动前必须先要结束B才可以,该这么办?
      

  4.   

    本来想给你们两为大哥分的,无奈老是 抱“Service Unavailable” 错误,后续补上!