How to Fire Ping Command In Java

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class comand {

public static void main(String[] args) {

comand obj = new comand();

String domainName = "google.com";



//in windows
String command = "ping -n 3 " + domainName;

String output = obj.executeCommand(command);

System.out.println(output);

}

private String executeCommand(String command) {

StringBuffer output = new StringBuffer();

Process p;
try {
p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader reader =
                            new BufferedReader(new InputStreamReader(p.getInputStream()));

                        String line = "";
while ((line = reader.readLine())!= null) {
output.append(line + "\n");
}

} catch (Exception e) {
e.printStackTrace();
}

return output.toString();

}

}
Previous
Next Post »

Disqus Shortname

Comments system