A Java “ping” program/class

Java ping FAQ: How do I ping a computer from a Java program (or Java class, or Java method)?

I've been working on a new Java networking application, and as part of network debugging, I wanted to be able to ping a server from my Java program. I thought writing a “Java ping” class/program would be straightforward, but in short, it wasn’t, so I wrote a little helper class to let me call the operating system ping command, and use the output from it. (Not what I wanted, but it works.)

I'm not going to get into the discussion too much today, as I'm still working on this class, so without much introduction, here's the source code for a Java ping class I've created. This class lets me ping other servers/hosts from my Java application, using the ping command from the host operating system. In my case, I'm using Mac OS X 10.5, so I'm using the Mac ping command, which is located in the /sbin directory.

My Java ping class (program) source code

Here's the source code for my JavaPingExampleProgram:

import java.io.*;
import java.util.*;

/**
 * A Java ping class.
 * Created by Alvin Alexander, devdaily.com.
 */
public class JavaPingExampleProgram
{

  public static void main(String args[]) 
  throws IOException
  {
    // create the ping command as a list of strings
    JavaPingExampleProgram ping = new JavaPingExampleProgram();
    List<String> commands = new ArrayList<String>();
    commands.add("/sbin/ping");
    commands.add("-c");
    commands.add("5");
    commands.add("www.google.com");
    ping.doCommand(commands);
  }

  /**
   * Provide the command you want to run as a List of Strings. Here's an example:
   * 
   * List<String> commands = new ArrayList<String>();
   * commands.add("/sbin/ping");
   * commands.add("-c");
   * commands.add("5");
   * commands.add("www.google.com");
   * exec.doCommand(commands);
   * 
   * @param command The command you want to execute, provided as List<String>.
   * @throws IOException This exception is thrown so you will know about it, and can deal with it.
   */
  public void doCommand(List<String> command) 
  throws IOException
  {
    String s = null;
    
    ProcessBuilder pb = new ProcessBuilder(command);
    Process process = pb.start();

    BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
    BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));

    // read the output from the command
    System.out.println("Here is the standard output of the command:\n");
    while ((s = stdInput.readLine()) != null)
    {
      System.out.println(s);
    }

    // read any errors from the attempted command
    System.out.println("Here is the standard error of the command (if any):\n");
    while ((s = stdError.readLine()) != null)
    {
      System.out.println(s);
    }
  }
  
}

As I mentioned, this Java ping class is still a work in progress, but it does work. As the main method shows, you can ping a network host like www.google.com by creating your system command as a List of String objects. The command shown in the code would look like this if issued on the command line:

/sbin/ping -c 5 www.google.com

Java ping class/program and InetAddress

As a final note before leaving today, I thought I'd be able to accomplish this task using the isReachable method of the InetAddress class, but that method didn't work as expected. For instance, while I am able to ping www.google.com from the command line, the isReachable method returns false when I try to access the same hostname. Therefore, I created my own Java ping program, as shown above.

Okay, one more final note: As you might guess from my Java ping class/program, I'm actually working to make this class more generic, so I can easily run any system command. Running the command is easy, but trying to determine what to do with standard output and standard error streams requires a little more thought, especially if you want to run your commands from a Java GUI app, and display the output in real time in the GUI.