Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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

  • Last Updated : 27 Aug, 2021

In article Pinging an IP address in Java, we have discussed how to ping an IP address using 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 program for ping using sub-process
import java.util.ArrayList;
public class PingWebsite
    // method for finding the ping statics 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
        BufferedReader Error = new BufferedReader(new InputStreamReader
        String s = null;
        System.out.println("Standard output: ");
        while((s = input.readLine()) != null)
        System.out.println("error (if any): ");
        while((s = Error.readLine()) != null)
    // Driver method
    public static void main(String args[]) throws Exception
        // creating list for commands
        ArrayList<String> commandList = new ArrayList<String>();
        // can be replaced by IP


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

--- 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 

Standard output: 
error (if any): 
ping: cannot resolve 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 GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks 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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!