Class NetworkUtil


  • public final class NetworkUtil
    extends Object
    Utility class that returns host name, MAC and IP.
    • Method Detail

      • hostname

        public static String hostname()
        Returns the host name if found.

        Sequence of HOSTNAME search:

        1. Try to find the HOSTNAME variable on LINUX environment;
        2. Try to find the COMPUTERNAME variable on WINDOWS environment;
        3. Try to find the host name by calling InetAddress.getLocalHost().getHostName() (the expensive way);
        4. If no host name is found, return null.
        Returns:
        a string containing the host name
      • mac

        public static String mac​(NetworkInterface nic)
        Returns the MAC address if found.

        Output format: "00-00-00-00-00-00" (in upper case)

        Returns:
        a string containing the MAC address
      • ip

        public static String ip​(NetworkInterface nic)
        Returns the IP address if found.

        Output format: "0.0.0.0" (if IPv4)

        Returns:
        a string containing the IP address
      • getMachineString

        public static String getMachineString()
        Returns a string containing host name, MAC and IP.

        Output format: "hostname 11-11-11-11-11-11 222.222.222.222"

        Returns:
        a string containing the host name, MAC and IP
      • nic

        public static NetworkInterface nic()
        Returns a network interface.

        It tries to return the network interface associated to the host name.

        If that network interface is not found, it tries to return the first network interface that satisfies these conditions:

        • it is up and running;
        • it is not loopback;
        • it is not virtual;
        • it has a hardware address.

        If no acceptable network interface is found, it returns null.

        Returns:
        a network interface.