Monday, January 6, 2014

How to ping a site or address with Java

Java does not natively have the ability to send ICMP messages, so we need to send them using the system's built in ping tool.

I added a simple ping utility to JavaUtils, which you can download the jar from SourceForge and GitHub:

JavaUtils JAR and source download on SourceForge
JavaUtils JAR and source download on GitHub

Then, you can test ping reliability with the built in isNetworkAddressReachable(String);

Here is how to use the JavaUtils class with NetBeans:

Select Properties of your project:

Under Libraries, select Add JAR/Folder

Select the javautils.jar, and click Choose.

Then, click OK.

Now, you're ready to use JavaUtils. Below is an example of its use:

import java.io.IOException;
import javautils.network.Ping;

public class Testing {
    
    public static void main(String[] args) {
        try {
            System.out.print("Is google.com reachable? ");
            System.out.println(Ping.isNetworkAddressReachable("google.com"));
        } catch (IOException ioe) {
            // Something is wrong with the network
        } catch (InterruptedException ie) {
            // The system waitFor process was interrupted
        }
    }
}

The output:

run:
Is google.com reachable? true
BUILD SUCCESSFUL (total time: 0 seconds)

1 comment:

  1. 1xbet korean bets today - Best legal betting online
    With one of the best sportsbook brands in the world and in a top-tier sportsbook, 1xbet can 1xbet 후기 be considered one of the most trustworthy online

    ReplyDelete