Checking an Internet Connection in Java
Submitted by donbermoy on Wednesday, November 12, 2014 - 10:20.
This is a tutorial in which we will going to create a program that can determine if your are connected in the internet or not using Java.
So, now let's start this tutorial!
1. Open JCreator or NetBeans and make a java program with a file name of checkInternetConnection.java.
2. Initialize your variable in your Main, variable p will be used in the Process class.
To create a process by pinging a website, have the syntax below:
A Process class executes run-time resources and will have its own resources. The syntax "ping" above indicates to call the Facebook website.
3. Now, we will now have the syntax of checking if the internet is connected or not. Have the code below:
We used method waitFor of the Process class variable for us to determine if there is an internet connection if it will be equal to 0.
The waitFor method used to wait for the process to be executed until such time that it will be terminated. If the output of the waitFor is 0, then the Internet is connected. Otherwise if it is 1, then there you are not connected to Internet.
Output:
Here's the full code of this tutorial:
Best Regards,
Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
If you have some queries, feel free to contact the number or e-mail below.
Landline: 826-9296
E-mail:[email protected]
Add and Follow me on Facebook: https://www.facebook.com/donzzsky
Visit and like my page on Facebook at: https://www.facebook.com/BermzISware
Add new comment
- 28 views