You can test the connection by setting the class’s Internet protocol InetAddress. If you get an exception or, for example, use getLocalHost()one that returns the local host address, give the following result:
localhost/127.0.0.1instead of a full name, for example, for example jaf-stephen-lenovoG40-80/152.6.44.13, then you are not connected to the Internet.
public static void main(String[] args) {
try {
InetAddress address = InetAddress.getByName("www.facebook.com");
System.out.println(address);
} catch (UnknownHostException e) {
System.err.println("Couldn't find www.facebook.com");
}
}
if you are connected to the Internet, you will receive the following output:
www.facebook.com/31.13.78.35
source
share