Java Network Interface Check isInterfaceUp(NetworkInterface iface)

Here you can find the source of isInterfaceUp(NetworkInterface iface)

Description

Determines, if possible, whether or not the iface interface is up.

License

LGPL

Parameter

Parameter Description
iface the interface that we'd like to determine as Up or Down.

Return

false if iface is known to be down and true if the iface is Up or in case we couldn't determine.

Declaration

public static boolean isInterfaceUp(NetworkInterface iface) 

Method Source Code

//package com.java2s;
/*//from  w  w w .j  a  v a2s .co  m
 * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
 * Maintained by the SIP Communicator community (http://sip-communicator.org).
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

import java.lang.reflect.*;
import java.net.*;

public class Main {
    /**
     * Determines, if possible, whether or not the <tt>iface</tt> interface is
     * up. We use this method so that we could use {@link
     * java.net.NetworkInterface}'s <tt>isUp()</tt> when running a JVM that
     * supports it and return a default value otherwise.
     *
     * @param iface the interface that we'd like to determine as Up or Down.
     *
     * @return <tt>false</tt> if <tt>iface</tt> is known to be down and
     * <tt>true</tt> if the <tt>iface</tt> is Up or in case we couldn't
     * determine.
     */
    public static boolean isInterfaceUp(NetworkInterface iface) {
        try {
            Method method = iface.getClass().getMethod("isUp");

            return ((Boolean) method.invoke(iface)).booleanValue();
        } catch (Throwable t) {
            //apparently we are not running in a JVM that supports the
            //isUp method. returning default value.
        }

        return true;
    }
}

Related

  1. dumpLocalNetworkInfo()
  2. hasNetworkAccess()
  3. isCIDR(String network)
  4. isHostInNetworkCard(String host)
  5. isInterfaceLoopback(NetworkInterface iface)
  6. isLoopbackNetworkInterface( NetworkInterface anInterface)
  7. isNetworkAvailable()
  8. isNetworkException(Exception e)
  9. isNetworkProblem(final Throwable ex)