views:

39

answers:

1

For some reason i cannot even phantom, Java does not have primitives for ICMPs and traceroute. Any idea how to overcome this? Basically im building code that should run in *nix and windows, and need a piece of code that will run in both platforms..

Thanks!

A: 

Unfortunately I can't find any direct access to ICMP or traceroute natively within Java.

The closest I've been able to find is the ability to determine reachability, which the API documentation says is likely to use ICMP echo requests.

This full example shows how to use the InetAddress class.

import java.net.InetAddress;

public class Ping {

public static void main(String[] args) {

    try {
        if (args.length != 1) {
            System.out.println("Usage: java Ping <hostname>");
                System.exit(-1);
        }

        String host = args[0];
        int timeout = 3000;
        boolean status = InetAddress.getByName(host)
                                .isReachable(timeout);

        System.out.println(host + ": reachable? " + status);

        } catch (java.net.UnknownHostException e) {
            e.printStackTrace();
        } catch (java.io.IOException ioe) 
            ioe.printStackTrace();
        }
    }
}

API Link: http://java.sun.com/j2se/1.5.0/docs/api/java/net/InetAddress.html

Helpful link: http://blog.taragana.com/index.php/archive/how-to-do-icmp-ping-in-java-jdk-15-and-above/

Gordon Christie