Friday, September 5, 2025
HomeLanguagesJavaPinging an IP address in Java | Set 2 (By creating sub-process)

Pinging an IP address in Java | Set 2 (By creating sub-process)

In article Pinging an IP address in Java, we have discussed how to ping an IP address using java.net.InetAddress.isReachable() method. In this post we will discuss how to execute ping command by creating a sub-process. 
Prerequisite : ProcessBuilder class , Process class
Below Java program creates a method commands() which take list of command(ping) as a parameter. As we know ProcessBuilder class is used to create operating system processes and ProcessBuilder.start() starts the sub-process which will execute the ping command. 
 

Java




// Java program for ping using sub-process
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
 
public class PingWebsite
{
    // method for finding the ping statistics of website
    static void commands(ArrayList<String> commandList)
                                           throws Exception
    {
        // creating the sub process, execute system command
        ProcessBuilder build = new ProcessBuilder(commandList);
        Process process = build.start();
         
        // to read the output
        BufferedReader input = new BufferedReader(new InputStreamReader
                                   (process.getInputStream()));
        BufferedReader Error = new BufferedReader(new InputStreamReader
                                   (process.getErrorStream()));
        String s = null;
         
        System.out.println("Standard output: ");
        while((s = input.readLine()) != null)
        {
            System.out.println(s);
        }
        System.out.println("error (if any): ");
        while((s = Error.readLine()) != null)
        {
            System.out.println(s);
        }
    }
     
    // Driver method
    public static void main(String args[]) throws Exception
    {
        // creating list for commands
        ArrayList<String> commandList = new ArrayList<String>();
         
        commandList.add("ping");
        // can be replaced by IP
        commandList.add("www.google.com");
         
        PingWebsite.commands(commandList);
    }
}


Output: 
 

Standard output: 
PING www.google.com (216.58.220.164): 56 data bytes
64 bytes from 216.58.220.164: icmp_seq=0 ttl=53 time=98.803 ms
64 bytes from 216.58.220.164: icmp_seq=1 ttl=53 time=87.856 ms
64 bytes from 216.58.220.164: icmp_seq=2 ttl=53 time=110.600 ms
64 bytes from 216.58.220.164: icmp_seq=3 ttl=53 time=92.897 ms
64 bytes from 216.58.220.164: icmp_seq=4 ttl=53 time=90.142 ms

--- www.google.com ping statistics ---
5 packets transmitted, 5 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 87.856/96.060/110.600/8.140 ms
error (if any): 

Reading and Understanding the Output: Each line to the destination represents a packet transmission, the time specified in milliseconds at the end is most relevant for testing an internet connection as a higher the number there indicates higher the lag is or a connection problem. If there is no response at all, either the server is down, there is a connection problem, it does not respond to ping requests, or it is very slow to respond. 
Output when there is no internet connection: The output of above program become very interesting when our system is not connected to internet 
Output: 
 

Standard output: 
error (if any): 
ping: cannot resolve www.google.com: Unknown host

Understanding the term “Packet loss”: Packet loss is high, you almost certainly have network issues, because packet loss means that data being sent between you and the server is being lost. The reason of pocket loss can be poor internet connection, wi-fi problems, general network problems, a bad connection, a struggling connection, an interfered connection, connection interruptions, or many other potential networking issues. 
  
Related Article : Pinging an IP address in Java 
This article is contributed by Abhishek Verma. If you like Lazyroar and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the Lazyroar main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 

RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6634 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11863 POSTS0 COMMENTS
Shaida Kate Naidoo
6750 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6701 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS