import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Ping { public static void pingCommand(String ping){ try { Process process=Runtime.getRuntime().exec(ping); try { process.waitFor();//等待子进程完成再往下执行 } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } BufferedReader br=new BufferedReader(new InputStreamReader( process.getInputStream())); String line=null; while((line=br.readLine())!=null){ System.out.println(line); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { Ping.pingCommand("ping 127.0.0.1"); } }