java本身实现ping命令比较麻烦,我现在用java来exec ping.exe 然后返回ping远方主机所得的信息,请问如何获取并处理ping命令返回在cmd窗口里的信息?即如何获取这些信息?如何读入?或如何存入一个txt中来分析?Pinging www.cache.split.netease.com [202.108.9.34] with 32 bytes of data:Reply from 202.108.9.34: bytes=32 time=26ms TTL=57
Reply from 202.108.9.34: bytes=32 time=27ms TTL=57
Reply from 202.108.9.34: bytes=32 time=27ms TTL=57
Reply from 202.108.9.34: bytes=32 time=26ms TTL=57Ping statistics for 202.108.9.34:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 26ms, Maximum = 27ms, Average = 26ms

解决方案 »

  1.   

    JNI 我估计JAVA本身实现不了  需要将系统的输出作为JAVA的输入
      

  2.   

    我也想获得shell的输出,不过有人指点说需要自己建立管道,过来学习
      

  3.   

    String cmd = "ping ....";
    Process p = Runtime.getRuntime().exec(cmd);
    InputStream input = p.getInputStream();
      

  4.   

    import java.io.*; 
    public class hello { /**
     * @param args
     */
    public static void main(String args[]) throws Exception 
        { 
            Process process = Runtime.getRuntime().exec("ping www.cache.split.netease.com"); 
            InputStream standardOutput = process.getInputStream(); 
                        
            int c; 
            String strOut = "";
           
            while ((c = standardOutput.read()) != -1) 
            {
             strOut += (char)c;
             //System.out.print((char)c); 
            }   
            System.out.print(strOut);
                
            standardOutput.close(); 
          
        } }
    管道比较简单,不过会有可能获取不全面 :
    C:\>ping www.cache.split.netease.com >test.txtC:\>type test.txtPinging www.cache.split.netease.com [220.181.28.51] with 32 bytes of data:Reply from 220.181.28.51: bytes=32 time=11ms TTL=57
    Reply from 220.181.28.51: bytes=32 time=13ms TTL=57
    Reply from 220.181.28.51: bytes=32 time=30ms TTL=57
    Reply from 220.181.28.51: bytes=32 time=11ms TTL=57Ping statistics for 220.181.28.51:
        Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
    Approximate round trip times in milli-seconds:
        Minimum = 11ms, Maximum = 30ms, Average = 16ms
      

  5.   

    不是,这个用exec()未必能获得所有的输出,例如linux中ps -Al | grep gnome未必可以将gnome字段的进程打印出来
      

  6.   

    yaray(雅睿,生活在别处) ( ) 信誉:110    Blog String cmd = "ping ....";
    Process p = Runtime.getRuntime().exec(cmd);
    InputStream input = p.getInputStream();
      

  7.   

    学习一下  
    用  Runtime.getRuntime()能够成功么??  各位尝试过了么??
      

  8.   

    /**
     *运行在windos命令行(cmd)中的命令
     */
    private final static String windowsRunCommand( ) throws IOException  
    {
    Process p = Runtime.getRuntime().exec("ipconfig /all"); // ping...
    InputStream stdoutStream = new BufferedInputStream             (p.getInputStream());
    StringBuffer buffer = new StringBuffer();
    for (;;)
    {
    int c = stdoutStream.read();
    if (c == -1) break;
    buffer.append((char) c);
    }
    String outputText = buffer.toString();
    stdoutStream.close();
    return outputText;
    }这是我程序中的代码。
    你也可以修改一下该函数增加一个字符串参数(命令字符串),也可以正确运行的^_^