Friday, December 5, 2008

Running a batch file in Java.

import java.io.*;
import java.lang.Runtime;

/**
*
* @author yamin Si
*/
public class testCallExe {
public testCallExe(){
Runtime r=Runtime.getRuntime();
Process p=null;
try
{
p = r.exec(new String[]{"cmd","/c","start C:/temp/test.bat"});
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));

String line=null;

while((line=input.readLine()) != null) {
System.out.println(line);
}
System.out.println("Exit Value = " p.waitFor());//Method waitFor() will make the current thread to wait until the external program finish and return the exit value to the waited thread.

}

catch(Exception e){
System.out.println("error===" e.getMessage());
e.printStackTrace();
}
}
public static void main(String args[]) {
testCallExe test=new testCallExe();
}
}

No comments: