Process pro = Runtime.getRuntime().exec("cmd /c ping localhost");

解决方案 »

  1.   

    写了个class,楼主看下吧
    import java.io.InputStream;
    import java.io.IOException;
    import java.lang.Runtime;
    import java.lang.RuntimeException;class Untitled2 {
      public static void main (String arg[]) {
        StringBuffer sb = new StringBuffer();
        Runtime run = Runtime.getRuntime();
        try {
          Process process = run.exec("ping 192.168.1.1");
          InputStream is = process.getInputStream();
          int t = 0;
          while ( (t = is.read()) != -1) {
            sb.append( (char)t);
          }
        } catch (IOException ex) {
          System.out.println("IOException : " +ex.toString());
        }
        System.out.println(sb.toString());
      }
    }
      

  2.   

    能不能加入一些参数-n -a -l之类的?比如说:ping -n 8 192.168.4.148
      

  3.   

    应该没有必要.EX:   ping 192.168.4.148
     俺可不敢抢分..和大家一起学习.要是楼主可怜就施舍点啦!
    各位大G包含!!嘿嘿!
      

  4.   

    可以加参数,Process process = run.exec("ping 192.168.1.1");
    里面字符串的内容就像你在cmd下输的dos命令一样
      

  5.   

    ping完之后还没有来得及看结果shell窗口就关闭了,能不能让shell窗口不关闭?
      

  6.   

    不管比哪个窗口我就不会了,不过还是可以通过下面的手段来观察执行结果的。如果你愿意也可以把输入定位到文件里啊什么的,随便你。    public static void main( String[] args ){
            try{
                Process pro = Runtime.getRuntime().exec("cmd /c ping localhost");
                BufferedReader dis = new BufferedReader(new InputStreamReader(pro.getInputStream()));
                String str = null;
                while( (str = dis.readLine()) != null ){
                    System.out.println( str );
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
      

  7.   

    pleonheart(只睡六小时),呵呵。你比我慢了7分钟诶。怎么能算我抢分啊:)
      

  8.   

    还有一个问题,我忘了问,就是我调用本地Ping以后,能不能在程序里直接去获得返回信息,比如是否Ping通,响应时间是多少,等等!