Like ICMP and traceroutes in Java

Java has no primitives for ICMP and traceroute. How to overcome this? Basically, I create code that should run on * nix and Windows, and I need a piece of code that will work on both platforms.

+6
java sockets icmp traceroute
source share
2 answers

Here is what I wrote today to β€œimplement” the route trace command in Java. I tested only on Windows, but it should also work on Linux, although several traceroute tools are available for Linux, so most likely you need some checks for these programs.

public class NetworkDiagnostics{ private final String os = System.getProperty("os.name").toLowerCase(); public String traceRoute(InetAddress address){ String route = ""; try { Process traceRt; if(os.contains("win")) traceRt = Runtime.getRuntime().exec("tracert " + address.getHostAddress()); else traceRt = Runtime.getRuntime().exec("traceroute " + address.getHostAddress()); // read the output from the command route = convertStreamToString(traceRt.getInputStream()); // read any errors from the attempted command String errors = convertStreamToString(traceRt.getErrorStream()); if(errors != "") LOGGER.error(errors); } catch (IOException e) { LOGGER.error("error while performing trace route command", e); } return route; } 
+4
source share

You will need the jpcap library (possibly SourceForge jpcap ) and use the ICMPPacket class to implement the desired functionality.

Here is an implementation of Java traceroute using the jpcap library .

+1
source share

All Articles