大哥,肯定是没有编译好的Ping class ,检查你运行时的命令行有没有错啊?

解决方案 »

  1.   

    看看下面的程序,我已经测试过,No problem!
    import java.io.*;
    import java.util.*;
    import java.net.*;
    public class Ping {  public static void main(String[] args){
       if(args.length!=1)
       {System.out.println("Usage:java Ping hostname");
        System.exit(1);
       }
       //Get the input hostname.
       String hostname=args[0];
       //DNS
       try{
           InetAddress addr=InetAddress.getByName(hostname);
           hostname=addr.getHostAddress();
        }catch(UnknownHostException ex)
        {  System.out.println("Unknown host:"+hostname);
           System.exit(1);
        }   //invoke the ping.exe
      try{
       Runtime r=Runtime.getRuntime();
       //Get the system property "Path",and set the envirenment of the process.
       Properties prop=System.getProperties();
       String path=prop.getProperty("java.library.path");
       String[] envp=new String[1];
       envp[0]="path="+path;
       //ping command and its arguments.
       String[] cmd=new String[2];
       cmd[0]="ping";
       cmd[1]=hostname;
       //execute it.
       Process p=r.exec(cmd,envp);   BufferedReader in=new BufferedReader(new InputStreamReader(p.getInputStream()));
       if(in!=null){
          boolean finished=false;
          while(!finished){
              String s=in.readLine();
              if(s!=null)
                { if(!s.trim().equalsIgnoreCase(""))
                      System.out.println(s);
                }
              else
                 finished=true;
            }//while
       }//if
       //wait for the process to end.
       int exitcode=p.waitFor();
       //System.out.println("ExitCode:"+exitcode);
     }catch(Exception ex)
     {System.out.println("Exception message:"+ex.getMessage());
      ex.printStackTrace();
     }//catch
     }
    }
      

  2.   

    程序名是ping or ping2 ?
    请在执行前加上路径参数 -cp
      

  3.   

    试过了,但是只能ping4次就自动断开连接了,怎么才能使他连续ping?
    谢谢
      

  4.   

    上次实现的是不待参数的ping.稍作修改就课和实际的ping完全一样。要连续ping的格式:java -cp ./ Ping -t www.csdn.net
    代码如下:
    import java.io.*;
    import java.util.*;
    import java.net.*;
    public class Ping {  public static void main(String[] args){
       int len=args.length;
       if(len<1)
       {System.out.println("Usage:java Ping [options] hostname");
        System.exit(1);
       }
       //Get the input hostname.
       String hostname=args[len-1];
       //DNS
       try{
           InetAddress addr=InetAddress.getByName(hostname);
           hostname=addr.getHostAddress();
        }catch(UnknownHostException ex)
        {  System.out.println("Unknown host:"+hostname);
           System.exit(1);
        }   //invoke the ping.exe
      try{
       Runtime r=Runtime.getRuntime();
       //Get the system property "Path",and set the envirenment of the process.
       Properties prop=System.getProperties();
       String path=prop.getProperty("java.library.path");
       String[] envp=new String[1];
       envp[0]="path="+path;
       //ping command and its arguments.
       String[] cmd=new String[len+1];
       cmd[0]="ping";   
       cmd[len]=hostname;
       /********新加的***************/
       for(int i=1;i<len;i++)
         cmd[i]=args[i-1];
       //execute it.
       /************************/
       Process p=r.exec(cmd,envp);   BufferedReader in=new BufferedReader(new InputStreamReader(p.getInputStream()));
       if(in!=null){
          boolean finished=false;
          while(!finished){
              String s=in.readLine();
              if(s!=null)
                { if(!s.trim().equalsIgnoreCase(""))
                      System.out.println(s);
                }
              else
                 finished=true;
            }//while
       }//if
       //wait for the process to end.
       int exitcode=p.waitFor();
       //System.out.println("ExitCode:"+exitcode);
     }catch(Exception ex)
      {System.out.println("Exception message:"+ex.getMessage());
      ex.printStackTrace();
      }//catch
     }
    }