使用java实现CMD最简单的ping功能

主要是使用untime.getRuntime().exec()来执行命令,但是可能会被杀毒软件拦截,慎用。

import java.io.*;

public class PingDemo {
    public static void main(String args[])
    {
        new MyThread().start();
    }
}

class MyThread extends Thread{
    public void run() {
        String line = null;
        try{
            Process pro = Runtime.getRuntime().exec("ping -l 65500 -t 192.168.178.37");
            BufferedReader buf = new BufferedReader(new InputStreamReader(pro.getInputStream()));
            while((line = buf.readLine()) != null)
                System.out.println(line);
        }catch(Exception ex){
            System.out.println(ex.getMessage());
        }
    }
}
0%