1.代码
public static String runCmd(String cmdpath, String cmd) {
log.info(cmd);
String result="";
File dir = new File(cmdpath);
try {
Process ps = Runtime.getRuntime().exec(cmd, null, dir);
BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream(), Charset.forName("GBK")));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
result+=line+"\n";
}
br.close();
System.out.println("close ... ");
ps.waitFor();
System.out.println("wait over ...");
return result;
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("child thread donn");
return null;
}
2.测试
public static void main(String args[]){
runCmd("C:\\","ping www.baidu.com");
}
3.结果
11-28 10:11:36.232 INFO [com.knife.doc.Utils.CmdUtils] - ping www.baidu.com
正在 Ping www.a.shifen.com [115.239.210.27] 具有 32 字节的数据:
来自 115.239.210.27 的回复: 字节=32 时间=3ms TTL=54
来自 115.239.210.27 的回复: 字节=32 时间=6ms TTL=54
来自 115.239.210.27 的回复: 字节=32 时间=4ms TTL=54
来自 115.239.210.27 的回复: 字节=32 时间=4ms TTL=54
115.239.210.27 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 3ms,最长 = 6ms,平均 = 4ms
close ...
wait over ...