怎么实现调用操作系统的命令如ping等 把cmd的命令返回信息在java窗口中输出.

wlboys 2008-01-03 08:51:09
怎么实现调用操作系统的命令如ping等 把cmd的命令返回信息在java窗口中输出.
...全文
161 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
ring03 2008-01-03
  • 打赏
  • 举报
回复

String IPAddre="127.0.0.1";
Process process = Runtime.getRuntime().exec("ping"+IPAddre);
BufferedReader message= new BufferedReader(new InputStreamReader(process.getInputStream());
while(message.readLine()!=null){
System.out.println(message);
}
mir2ming 2008-01-03
  • 打赏
  • 举报
回复
使用Java的Runtime 就可以了,代码如下:
/*
* Created on Jan 3, 2008
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package org.mytech.test;

import java.io.*;

/**
* @author 806883
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/

public class RunPing {

public static void main(String[] argv) {
try {
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("ping www.163.com");
BufferedReader reader = new BufferedReader(new InputStreamReader(
process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧