现在的位置: 首页 > 综合 > 正文

java获得cmd命令结果

2013年03月22日 ⁄ 综合 ⁄ 共 431字 ⁄ 字号 评论关闭

运行一个外部程序捕获并输出  

首先运行ping程序,然后把它的输出打印到屏幕上。

class Main {
 public static void main(String[] args) {
   try {
     String cmd = "ping ";
String param ="202.112.58.200";
     Process child = Runtime.getRuntime().exec(cmd+param);
     // 获得ping的输出
    InputStream child_in = child.getInputStream();
     int c;
     while ((c = child_in.read()) != -1) {
//   System.out.println("kkk");
       System.out.print((char)c);
     }
     child_in.close();
   } catch (IOException e) {
     System.err.println(e);
   }
 }
}

抱歉!评论已关闭.