Thursday, September 4, 2025
HomeLanguagesJavaChecking Internet Connectivity using Java

Checking Internet Connectivity using Java

Checking Internet connectivity using Java can be done using 2 methods:
1) by using getRuntime() method of java Runtime class.
2) by using methods of java URL and URLConnection classes.

#Java Runtime Class:This class is used to interact with java runtime environment (Java virtual machine) in which the application is running. It provides methods/functions to execute a process or a command, invoke garbage collector, get total and free memory in the JVM etc.

#getRuntime():This method of java runtime class returns the runtime object associated with the current java application.
you can learn more about this class here

#Java URL Class:This class provides methods that returns various information like protocol, hostname, file Name, port Number etc of the URL.

#Java URLConnection class:It represents a link between URL and application and can be used to read and write data to the specified resource referred by the URL.

#openConnection():This method of java URLConnection class opens the connection to the specified URL.
you can learn more about these classes here and here

NOTE: Provided method should be run on a local machine and not on an online compiler.
METHOD 1:
Output by this method will be 0 if the internet is connected and it will be 1 if the internet is not connected.




// Java program for Checking Internet connectivity
import java.util.*;
import java.io.*;
  
class checking_internet_connectivity {
    public static void main(String args[]) throws Exception
    {
        Process process = java.lang.Runtime.getRuntime().exec("ping www.geeksforgeeks.org");
        int x = process.waitFor();
        if (x == 0) {
            System.out.println("Connection Successful, "
                               + "Output was " + x);
        }
        else {
            System.out.println("Internet Not Connected, "
                               + "Output was " + x);
        }
    }
}


OUTPUT:
validating connectivity

METHOD 2:
If Internet is not connected it will throw an exception and catch will execute printing respective message.




// Java program for checking Internet connectivity
import java.util.*;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
  
class checking_internet_connectivity {
    public static void main(String args[])
    {
        try {
            URL url = new URL("https://www.geeksforgeeks.org/");
            URLConnection connection = url.openConnection();
            connection.connect();
  
            System.out.println("Connection Successful");
        }
        catch (Exception e) {
            System.out.println("Internet Not Connected");
        }
    }
}


OUTPUT:
validating connectivity

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32261 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11797 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6748 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6695 POSTS0 COMMENTS
Umr Jansen
6716 POSTS0 COMMENTS