In this article lets discuss about how to ping a host using java.
Consider a scenario where you need to determine whether a host is reachable from your machine or not. You can easily find it by using ping command in Windows or ipconfig in Linux systems. But what if this is part of your requirement in your application project. It can be easily done using java.net package available as a part of java api.
Consider the following program which pings a host.
public class PingPoller
public static void main(String args)
System.out.println("Ping Poller Starts...");
String ipAddress = "localhost";
InetAddress inet = InetAddress.getByName(ipAddress);
System.out.println("Sending Ping Request to " + ipAddress);
boolean status = inet.isReachable(5000); //Timeout = 5000 milli seconds
System.out.println("Status : Host is reachable");
System.out.println("Status : Host is not reachable");
catch (UnknownHostException e)
System.err.println("Host does not exists");
catch (IOException e)
System.err.println("Error in reaching the Host");
Here is the sample output.
Sending Ping Request to localhost
Status : Host is reachable
In this way any host can be pinged from a java program.