我想做一个java窗口界面简单的实现ping命令,可以调用系统的ping命令,界面中只要输入要ping的ip地址和ping的次数就可以在下面显示响应的回应信息.哪位帮帮忙,急用!做一个参考!发到  再次谢谢了!

解决方案 »

  1.   

      public static void main(String[] args) throws IOException {
        Runtime run = Runtime.getRuntime();
        Process pro = run.exec("ping 127.0.0.1");
        BufferedReader br = new BufferedReader(new InputStreamReader(pro.getInputStream()));
        String str = "";
        while ((str = br.readLine()) != null) {
          System.out.println(str);
        }
        br.close();
        pro.destroy();
      }
      

  2.   

    try {
    Process proc = Runtime.getRuntime().exec("ping 127.0.0.1"); BufferedReader bufferedReader = new BufferedReader(
    new InputStreamReader(proc.getInputStream()));
    String line = null;
    while ((line = bufferedReader.readLine()) != null)
    System.out.println(line);
    bufferedReader.close(); //System.out.println();
    } catch (IOException e) {
    e.printStackTrace();
    }
      

  3.   

    我想问的是 怎样实现调用cmd后也把cmd的输出做为java窗口中的输出.
      

  4.   

    你把上面那两位的代码稍改一下不就得了,我帮你把它改好了,也发到你邮箱了import java.awt.*;
    import java.awt.event.*;
    import java.io.*;public class PingTest extends Frame implements ActionListener
    {
    TextField field1 = new TextField(12);
    TextField field2 = new TextField(3);
    TextArea result = new TextArea(30,50);
    public PingTest()
    {
    setSize(400,600);
    setTitle("Ping命令");
    setVisible(true);
    setLayout(new FlowLayout());
    setResizable(false);
    addWindowListener(new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    });
    Label label1 = new Label("Ping");
    Label label2 = new Label("次");
    Button btn = new Button("返回结果");
    btn.addActionListener(this);
    result.setEditable(false);

    add(label1);
    add(field1);
    add(field2);
    add(label2);
    add(btn);
    add(result);

    validate();
    }
    public static void main(String[] args)
    {
    new PingTest();
    }

    public void actionPerformed(ActionEvent e)
    {
    try   
    {
    int pingNum=4;
    if(!field2.getText().trim().equals(""))
    pingNum=Integer.parseInt(field2.getText());
    Process proc = Runtime.getRuntime().exec("ping "+field1.getText()+" -n "+pingNum);
    BufferedReader bufferedReader = new BufferedReader( 
    new InputStreamReader(proc.getInputStream())); 
    String line = null; 
    while((line=bufferedReader.readLine())!=null) 
    result.append(line+"\n"); 
    bufferedReader.close();
    }   
    catch(IOException e2)   

    e2.printStackTrace(); 
    }
    }
    }
      

  5.   

    这个小东东我实现过的,我实现的是只要你输入cmd命令就有反馈结果,还能像cmd窗口一样输入命令的.
    代码的文章URL:http://blog.csdn.net/savechina/archive/2007/12/29/2003203.aspx
    代码如下:
    最近有个想法,用Java实现像Windows 下CMD控制台一样,可以进行交互式地运行命令进行操作,今天终于先写了个简单的例子.现将其发布出来与大家分享.代码如下:package jconsole;import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.Scanner;/**
     * <title>"JConsole"-Java模拟控制台</title>
     * <p> 
     * 这个类是用Java实现Windows CMD控制台。通过获得当前运行进程,
     * 执行相关命令信息。实现交互式的CMD控制台。<br>
     * <br>
     * 最近有个想法,用Java实现像Windows 下CMD控制台一样,可以进行交互式地运行命令进行操作,
     * 今天终于写了个简单的例子。
     * 
     * </p>
     * @author 魏仁言
     * @version 0.9
     */
    public class JConsole {
     // private Process proConsole; /**
      * 获取执行当前命令的进程
      * 
      * @param strCommand
      *            运行命令
      * @return pro Precess
      */
     private Process getProcess(String strCommand) {
      Process pro = null;
      try {
       // 运行当前命令并获得此运行进程
       pro = Runtime.getRuntime().exec(strCommand);
      } catch (IOException e) {
       // when cause IOException ,Print the Exception message;
       System.out.println("Run this command:" + strCommand
         + "cause Exception ,and the message is " + e.getMessage());
      }
      // 返回运行命令的进程
      return pro;
     } /**
      * 获取当前进程标准输入流
      * 
      * @param pro
      *            当前进程
      * @return in InputStream
      */
     public InputStream getInputSreamReader(Process pro) {
      InputStream in = null;  // 获得当前进程的输入流
      in = pro.getInputStream();  // 返回输入流
      return in;
     } /**
      * 获取当前进程的错误输入流
      * 
      * @param pro
      *            当前进程
      * @return error InputStream
      */
     public InputStream getErrorSreamReader(Process pro) {
      InputStream error = null;  // 获得当前进程的错误流
      error = pro.getErrorStream();  // 返回错误流
      return error;
     } /**
      * 打印输入流中的内容到标准输出
      * 
      * @param in
      *            InputStream 输入流
      */
     public void printMessage(final InputStream in) {
      Runnable runnable = new Runnable() {   /*
        * (non-Javadoc)
        * 
        * @see java.lang.Runnable#run()
        */
       public void run() {
        try {
         /*
          * int ch; do { ch = in.read();
          * 
          * if (ch < 0) return; System.out.print((char) ch);
          * System.out.flush(); } while (true);
          */
         BufferedReader buffer = new BufferedReader(
           new InputStreamReader(in));
         String lines;
         while (true) {
          lines = buffer.readLine();
          if (lines == null)
           return;
          System.out.println("" + lines);
          System.out.flush();
         }    } catch (Exception e) {
         e.printStackTrace();
        }   }  };
      Thread thread;
      thread = new Thread(runnable);
      thread.start();
     } /**
      * 初始化CMD控制台。
      */
     public void run() {
      Process pro;  InputStream input;
      pro = getProcess("cmd");  input = getInputSreamReader(pro);  printMessage(input);
     } /**
      * @param args
      * @throws IOException
      */
     public static void main(String[] args) throws IOException {
      // 运行子进程
      Process pro = null;  Scanner in = new Scanner(System.in);  // 输入流
      InputStream input;  // 错误流
      InputStream err;  JConsole jconsole = new JConsole();  // 初始化CMD控制台
      jconsole.run();  // 从输入中取得要执行的命令,并打印反馈结果信息
      while (in.hasNextLine()) {   String strCMD = in.nextLine();   if (strCMD.equals("exit")) {
        System.exit(0);
       }   // 执行输入的命令
       pro = jconsole.getProcess("cmd /E:ON /c " + strCMD);   input = jconsole.getInputSreamReader(pro);   err = jconsole.getErrorSreamReader(pro);   // 打印命令运行结果信息
       int strTmp = input.read();
       if (strTmp == -1) {
        jconsole.printMessage(err);
       } else {
        jconsole.printMessage(input);
       }
      }
     }}
      

  6.   

    如果我要ping一个网段内的ip地址,是不是做一个循环就可以了?
    谢谢大家的回复!
      

  7.   

    import java.io.*;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class Ping { public static void main(String[] args){
    String lineout = null;
    String reg = "Received\\s=\\s\\d";
    String catchval = null;
    try {
    Process proc = Runtime.getRuntime().exec("ping 1.1.110");

    BufferedReader bufferedReader = new BufferedReader(
    new InputStreamReader(proc.getInputStream()));
    String line = null;
    while ((line = bufferedReader.readLine()) != null)
    // System.out.println(line);
    lineout += line + "\n";
    bufferedReader.close();

    } catch (IOException e) {
    e.printStackTrace();
    }

    Pattern p = Pattern.compile(reg);
    Matcher m = p.matcher(lineout);
    if(m.find()){

    System.out.println(catchval = m.group());
    if(catchval.charAt(11) == '4') {

    System.out.print("Ping successfully");
    }
    else
    {
    System.out.println("Ping fail");
    }
    }
    else{

    System.out.println("not find");
    System.out.println("fail");
    }




    }