Java Program to Check Internet Connectivity

In this article you will see how to check internet connectivity by using java programming language.

Java Program to Check Internet Connectivity

What is Internet?

Internet refers to the global network means global system of interconnected computer networks. It is also called as network of network. In short we say it only ‘net’. It allows us to connect with any other computer or network devices globally.

What is internet connectivity?

Internet connectivity refers to the technology through which we can connect to any network device and establish a communication with that device. By using computer terminals or network devices you can connect to internet and avail various internet services.

Let’s see the program to understand it more clearly.

NOTE: Please run the program on local machine and not on an online compiler to see the correct output.

Method-1: Java Program to Check Internet Connectivity By Using Java URL Class

Approach:

  • Create an object of URL class and pass the web link as parameter
  • Check the connection is getting established or not by using openConnection() method of URLConnection class.
  • If connection is getting established then Internet is connected else it will throw an exception and catch block will be executed which will print the result as internet is not connected.

Output:

package btechgeeks;

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

public class CheckInternetConnection 
{
        public static void main(String args[]) throws Exception
        {
        	try 
        	{
        		//Create an object of URL class and pass the web link as parameter
        	        URL obj = new URL("https://www.google.com");
        	        //Check the connection is getting established or not 
        	        //By using openConnection() method of URLConnection class
        	        URLConnection con = obj.openConnection();
        	        con.connect();   
        	 
        	        System.out.println("Internet Connection Available");   
        	            
        	 }
        	//if connection can't be established
        	//then internet is not connected
        	catch (Exception e)
        	{  
        	        System.out.println("No Internet Connection Available");     
        	                                                            
        	} 
        }
}

Output:

Internet Connection Available

Method-2: Java Program to Check Internet Connectivity By Using Java Runtime Class

Approach:

  • First get the run time object of running application by using getRuntime() method of Runtime class.
  • Check internet connectivity by running a simple ping to OpenDNS’s IP i.e 208.67.222.222.
  • You will get an output as 0 if the internet connection is available else you will get 1.
  • Them match the output by using an if condition, if the out put is 0 then print ‘Internet is connected’ else print ‘Internet is not connected’.

Output:

package btechgeeks;

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

public class CheckInternetConnection 
{
        public static void main(String args[]) throws Exception
        {
        	//By using  getRuntime() method test connectivity
            Process process = java.lang.Runtime.getRuntime().exec("ping -n 1 208.67.222.222");
            int output = process.waitFor();
            //output will be 0 if internet connectivity is available
            if (output == 0) 
            {
                System.out.println("Internet Connected, "
                                   + "Output is " + output);
            }
            //else output will be 1
            else 
            {
                System.out.println("Internet Not Connected, "
                                   + "Output is " + output);
            }
        }
}

Output:

Internet Connected, Output is 0

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.