public class ExecDemo { static public String[] runCommand(String cmd) throws IOException {
// set up list to capture command output lines
ArrayList list = new ArrayList();
// start command running
Process proc = Runtime.getRuntime().exec(cmd); /**译者注:前面的声明应该改成java.lang.Process,即: java.lang.Process proc = Runtime.getRuntime().exec(cmd); 如果不改的话可能编译不同通过,在译者的机器上使用jdk1.2,编译出现5个错误 使用jdk1.4编译出现4个错误 */ // get command´s output stream and // put a buffered reader input stream on it
InputStream istr = proc.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(istr));
// read output lines from command
String str; while ((str = br.readLine()) != null) list.add(str);
Copyright 2001-2010, www.comprg.com.cn, All Rights Reserved 京ICP备14022230号-1,电话/传真:010-82561037 82561614 ,Mail:gaojian@comprg.com.cn
地址:北京市海淀区远大路20号宝蓝大厦E座704,邮编:100089