JDK-6709430 : Unable to detect MAC address in Windows Vista
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 6
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_vista
  • CPU: x86
  • Submitted: 2008-06-02
  • Updated: 2011-02-16
  • Resolved: 2009-03-26
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
Java 6 update 6

ADDITIONAL OS VERSION INFORMATION :
Windows Vista

A DESCRIPTION OF THE PROBLEM :
I am unable to detect the MAC address(es) on a machine using Windows Vista.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
See attached source code

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
A correctly formatted MAC address (for ex: 00-16-41-44-F5-A2)

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
Here is the snippet of code that I'm using (which works fine under windows xp profesional/home edition)
...
private static final String FORMAT = "%02X"; //$NON-NLS-1$
private static final String SEPARATOR = "-"; //$NON-NLS-1$
...
   /**
     * Returns the first mac address found.
     *
     * @return the mac address or undefined
     */
    public String getMacAddress()
    {

        Enumeration<NetworkInterface> nis;
        try
        {
            nis = NetworkInterface.getNetworkInterfaces();
        }
        catch (SocketException e)
        {
            // can't get network interfaces
            // we won't be able to retrieve the MAC
            _plugin.getLog().log(getErrorStatus(getResourceString("security.error"), e)); //$NON-NLS-1$
            return null;
        }
        // not all interfaces will have a mac address
        // for instance loopback on windows
        while (nis.hasMoreElements())
        {
            String mac = null;
            try
            {
                mac = macToString(nis.nextElement());
            }
            catch (SocketException ex)
            {
                // could not retrieve MAC from this network interface
                // oh well let's log it and try the next interface
                _plugin.getLog().log(getErrorStatus(getResourceString("security.error"), ex)); //$NON-NLS-1$
                continue;
            }
            if (mac != null)
                return mac;
        }

        return null;
    }
...
    public static String macToString( NetworkInterface ni ) throws SocketException
    {
        return macToString( ni, SEPARATOR,  FORMAT );
    }

    /**
     * Returns the hardware address of the given network interface
     *
     * @param ni - the network interface
     * @param separator - byte separator
     * @param format - byte formatter
     * @return String - the mac address as a string
     * @throws SocketException
     */
    public static String macToString( NetworkInterface ni, String separator, String format ) throws SocketException
    {
        byte mac [] = ni.getHardwareAddress();

        if( mac != null ) {
            StringBuffer macAddress = new StringBuffer();
            String sep = StringUtils.EMPTY;
            for( byte o : mac ) {
                macAddress.append( sep ).append( String.format( format, o ) );
                sep = separator;
            }
            return macAddress.toString();
        }

        return null;
    }

---------- END SOURCE ----------

Comments
EVALUATION It looks like the issue here (as well as in related reports) is that the test is expecting the first NetworkInterface (returned by NetworkInterface.getNetworkInterfaces()) to have a valid MAC address. Not all interfaces have MAC addresses (eg loopback) and it seems different versions of Windows return the interfaces in different order. So, this is not a bug (though it has already been closed as a duplicate)
21-04-2009

EVALUATION Likely to be same issue as 6763420
26-03-2009