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.
Standard output: PING www.google.com (184.108.40.206): 56 data bytes 64 bytes from 220.127.116.11: icmp_seq=0 ttl=53 time=98.803 ms 64 bytes from 18.104.22.168: icmp_seq=1 ttl=53 time=87.856 ms 64 bytes from 22.214.171.124: icmp_seq=2 ttl=53 time=110.600 ms 64 bytes from 126.96.36.199: icmp_seq=3 ttl=53 time=92.897 ms 64 bytes from 188.8.131.52: 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 transission, 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 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 GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Pinging an IP address in Java | Set 1
- Finding IP address of a URL in Java
- Java program to find IP address of your computer
- How to validate an IP address using Regular Expressions in Java
- Check if email address valid or not in Java
- Creating Frames using Swings in Java
- Creating Sequential Stream from an Iterator in Java
- Java Swing | Creating a Toast Message
- Creating interactive maps and Geo Visualizations in Java
- Java Swing | Creating Custom Message Dialogs
- Creating an Asynchronous Multithreaded chat Application in Java
- Creating Sheets in Excel File in Java using Apache POI
- Containerizing Java applications | Creating a Spring Boot App using Dockerfile
- Creating a Cell at specific position in Excel file using Java
- Image Processing in Java | Set 7 (Creating a random pixel image)
- Image Processing in Java | Set 8 (Creating mirror image)
- Android | Creating a SeekBar
- Creating a file using FileOutputStream
- Android | Creating a RatingBar
- Android | Creating multiple Screen app