java+netbeans编一个ping的小程序,请高手指教!
要求程序能够类似ping的命令ping网络,还能使用ipconfig,tracert,等命令,
可以针对不同的ip定时定制任务

解决方案 »

  1.   

    参考代码:public class Test{
    /*
     *执行命令
     *@param cmd 要执行的命令
     * 
     * */
    public static void execCmd(String cmd) throws IOException 
    {
    Process p=Runtime.getRuntime().exec("cmd /c  "+cmd);
    BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line=null;
        while((line=br.readLine())!=null)
        {
         System.out.println(line);
        }
        br.close();
    }public static void main(String[] args)throws Exception { 
     execCmd("ipconfig");

    } }
    程序执行结果:
    Windows IP ConfigurationEthernet adapter 本地连接:        Media State . . . . . . . . . . . : Media disconnectedEthernet adapter 内部无线网卡:        Connection-specific DNS Suffix  . :         IP Address. . . . . . . . . . . . : 192.168.1.187        Subnet Mask . . . . . . . . . . . : 255.255.255.0        Default Gateway . . . . . . . . . : 192.168.1.1
      

  2.   

    试了,很好用,谢谢。
    不过这句是什么意思?
    BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));
      

  3.   

    BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));
    这个是把输入流读到字节缓冲区中,主要是为了提高性能,也可用不用,直接用InputStreamReader。
    以下是我以前看到的解决办法:String[] cmd = new String[5];   
    cmd[0] = "cmd";   
    cmd[1] = "/c";   
    cmd[2] = "start";   
    cmd[3] = " ";   
    cmd[4] = filePath;   
    Process process = Runtime.getRuntime().exec(cmd);  
    String[] cmd = new String[5];
    cmd[0] = "cmd";
    cmd[1] = "/c";
    cmd[2] = "start";
    cmd[3] = " ";
    cmd[4] = filePath;
    Process process = Runtime.getRuntime().exec(cmd);
    或; 
    Java代码 
    Process process = Runtime.getRuntime().exec("cmd /c start \"\" \"E:\\kk sd\\www.txt\"");  
    Process process = Runtime.getRuntime().exec("cmd /c start \"\" \"E:\\kk sd\\www.txt\"");
    因为按找文档说明 start 命令之后首先是[title],再是[filepath],所以将title设置为 " " 
    调用外部程序来打开一个相应的文件 
    比如我们要使用Editplus来打开一个远程机器上的文件,可以这样 
    Java代码 
    Runtime.getRuntime().exec("D:\\EditPlus 2\\EditPlus.exe"+" "+\\\\172.16.1.6\\server1\\SystemErr.log)即   
    Runtime.getRuntime().exec("外部程序位置"+" "+"要打开的文件").  
    Runtime.getRuntime().exec("D:\\EditPlus 2\\EditPlus.exe"+" "+\\\\172.16.1.6\\server1\\SystemErr.log)即
    Runtime.getRuntime().exec("外部程序位置"+" "+"要打开的文件").以此方式可以忽略空格的问题 截取控制台的信息. 
    使用JAVA输入流的方式. Java代码 
    Runtime.getRuntime().exec(..)获取的Process;   
    Process pro = Runtime.getRuntime().exec(..);   
    InputStreamReader isr = new InputStreamReader (pro.getInputStream());   
    //todo 使用输入流进行操作就可以了.  
    Runtime.getRuntime().exec(..)获取的Process;
    Process pro = Runtime.getRuntime().exec(..);
    InputStreamReader isr = new InputStreamReader (pro.getInputStream());
    //todo 使用输入流进行操作就可以了.另外JDK6也提供了一种方式 Java代码 
    Desktop dsk=DeskTop.getDesktop();   
    dsk.open(new File(filePath));
      

  4.   

    程序只是直接用JAVA调用 WINDOWS 的ipconfig命令。。并非用JAVA实现获取IP信息。。