How do I ping a host?

Since Java 1.5 (Tiger) the class introduces isReachable() method that can be use to ping or check if the address specified by the InetAddress is reachable.



public class PingExample {
    public static void main(String[] args) {
        try {
            InetAddress address = InetAddress.getByName("");

            // Try to reach the specified address within the timeout
            // period. If during this period the address cannot be
            // reach then the method returns false.
            boolean reachable = address.isReachable(10000);

            System.out.println("Is host reachable? " + reachable);
        } catch (Exception e) {

Wayan Saryada

Founder at Kode Java Org
I am a programmer, a runner, a recreational diver, currently live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. If these posts help, you can support me, buy me a cup of coffee or tea. Thank you 🥳

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.