Java Network Interface Check isNetworkAvailable()

Here you can find the source of isNetworkAvailable()

Description

Short method to check whether or not the user has an active internet connection

License

Open Source License

Declaration

public static boolean isNetworkAvailable() 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class Main {
    /**//from   w w w  . j a  v  a2  s  .  c om
     * Short method to check whether or not the user has an active internet connection
     */
    public static boolean isNetworkAvailable() {
        try {
            final URL url = new URL("http://www.google.com");
            final URLConnection conn = url.openConnection();
            conn.connect();
            return true;
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            return false;
        }
    }
}

Related

  1. isCIDR(String network)
  2. isHostInNetworkCard(String host)
  3. isInterfaceLoopback(NetworkInterface iface)
  4. isInterfaceUp(NetworkInterface iface)
  5. isLoopbackNetworkInterface( NetworkInterface anInterface)
  6. isNetworkException(Exception e)
  7. isNetworkProblem(final Throwable ex)
  8. isUpAndNotLoopback(final NetworkInterface ni)
  9. printInterface(NetworkInterface netIf)