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.
NOTE: Provided method should be run on a local machine and not on an online compiler.
Output by this method will be 0 if the internet is connected and it will be 1 if the internet is not connected.
If Internet is not connected it will throw an exception and catch will execute printing respective message.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Size of file on the Internet using Java
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java.util.Collections.rotate() Method in Java with Examples
- Java.util.LinkedList.peek() , peekfirst(), peeklast() in Java
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.util.concurrent.Phaser class in Java with Examples
- Java.util.concurrent.RecursiveTask class in Java with Examples
- Java.util.LinkedList.offer(), offerFirst(), offerLast() in Java
- Java.util.function.IntPredicate interface in Java with Examples
- Java.util.function.BiPredicate interface in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
- Java Deprecated API Scanner tool (jdepscan) in Java 9 with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java.util.function.LongPredicate interface in Java with Examples
- Java.lang.Short toString() method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java.util.function.DoublePredicate interface in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.