Java Utililty Methods Network Interface Check

List of utility methods to do Network Interface Check

Description

The list of methods to do Network Interface Check are organized into topic(s).

Method

booleanisLoopbackNetworkInterface(NetworkInterface anInterface)
is Loopback Network Interface
if (anInterface == null)
    return false;
if (isLoopbackMethod != null) {
    try {
        return (Boolean) isLoopbackMethod.invoke(anInterface);
    } catch (Throwable t) {
boolean hasLoopback = false;
Enumeration<InetAddress> allIntfAddr = anInterface.getInetAddresses();
while (allIntfAddr.hasMoreElements()) {
    InetAddress anAddr = allIntfAddr.nextElement();
    if (anAddr.isLoopbackAddress()) {
        hasLoopback = true;
        break;
return hasLoopback;
booleanisNetworkAvailable()
Short method to check whether or not the user has an active internet connection
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) {
...
booleanisNetworkException(Exception e)
is Network Exception
if (e == null) {
    return false;
if (e instanceof SocketException) {
    return true;
if (e instanceof SocketTimeoutException) {
    return true;
...
booleanisNetworkProblem(final Throwable ex)
is Network Problem
if (ex == null) {
    return false;
} else if (ex instanceof SocketTimeoutException || ex instanceof SocketException) {
    return true;
} else {
    return isNetworkProblem(ex.getCause());
booleanisUpAndNotLoopback(final NetworkInterface ni)
is Up And Not Loopback
return ni != null && !ni.isLoopback() && ni.isUp();
voidprintInterface(NetworkInterface netIf)
print Interface
out.printf("Display name: %s\n", netIf.getDisplayName());
out.printf("Name: %s\n", netIf.getName());
displaySubInterfaces(netIf);
out.printf("\n");
MapretrieveNetworkInterfaces()
Retrieves all InetAddress for the running VM, indexed by their name.
final Map<String, InetAddress> namedAddresses = new HashMap<String, InetAddress>();
final Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
for (NetworkInterface netint : Collections.list(nets)) {
    final Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
    for (InetAddress inetAddress : Collections.list(inetAddresses)) {
        if ((inetAddress instanceof Inet6Address) || inetAddress.isAnyLocalAddress()
                || inetAddress.isLinkLocalAddress() || inetAddress.isLoopbackAddress()
                || inetAddress.isMulticastAddress()) {
...
voidsortInterfaces(List interfaces)
sort Interfaces
Collections.sort(interfaces, new Comparator<NetworkInterface>() {
    @Override
    public int compare(NetworkInterface o1, NetworkInterface o2) {
        return Integer.compare(o1.getIndex(), o2.getIndex());
});