JDK-8021380 : NetworkInterface.getHardwareAddress does not report proper addresses
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 7u4,7u11
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_7
  • Submitted: 2013-07-22
  • Updated: 2022-12-01
  • Resolved: 2022-12-01
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdResolved
Related Reports
Duplicate :  
Duplicate :  
Relates :  
Description
FULL PRODUCT VERSION :
java version  " 1.7.0_25 " 
Java(TM) SE Runtime Environment (build 1.7.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.1.7601]

A DESCRIPTION OF THE PROBLEM :
This is essentially the same problem described in JDK-6763420, but it's occurring under Windows 7.
Multiple MAC addresses are reported by 'ipconfig /all', only one is reported by java.lang.NetworkInterface.
The repro code below reports the same number of interfaces from NetworkInterface and ipconfig, but the list from NetworkInterface repeats a single address.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile and run the repro program below.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -

MAC addresses derived from NetworkInterface:
   (your MAC addr)
   (your other MAC addr)

MAC addresses derived from ipconfig:
   (your MAC addr)
   (your other MAC addr)

(Actual number of addresses and address values will vary.)
ACTUAL -

MAC addresses derived from NetworkInterface:
   (your other MAC addr)
   (your other MAC addr)

MAC addresses derived from ipconfig:
   (your MAC addr)
   (your other MAC addr)

(The final address from ipconfig is consistently the only one listed by NetworkInterface.)

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
/*
 *  Demonstrate a bug in java.net.NetworkInterface class on Windows.
 *  This class uses Windows-specific 'ipconfig' command.
 */
import java.net.NetworkInterface;
import java.net.SocketException;
import java.io.*;
import java.util.*;

public class NetInterfaceBug {

    public static void main(String[] inArgs) {
        NetInterfaceBug subject = new NetInterfaceBug();
        subject.dummpNetworkInterfaceMacs();
        subject.dumpIpConfigMacs();
    }

    void dummpNetworkInterfaceMacs() {
        ArrayList<String> macAddrs = new ArrayList<String>();
        try {
            Enumeration niEnum = NetworkInterface.getNetworkInterfaces();
            while ( niEnum.hasMoreElements() ) {
                String macAddr = extractMac( ( NetworkInterface ) niEnum.nextElement() );
                if ( macAddr != null && macAddr.length() > 0 ) {
                    macAddrs.add( macAddr );
                }
            }
        } catch ( Exception e ) {
            System.err.println(  " can't get network interfaces:  "  + e );
        }
        dumpMacs( macAddrs,  " NetworkInterface "  );
    }

    String extractMac(NetworkInterface inNi) throws SocketException {
        byte[] addrBytes = inNi.getHardwareAddress();
        StringBuilder addrBuff = new StringBuilder();
        if ( addrBytes != null && addrBytes.length == 6 ) {
            for ( int ndx = 0; ndx < addrBytes.length; ndx++ ) {
                String sep = ( ndx == 0 ) ?  "  "  :  " - " ;
                addrBuff.append( String.format(  " %s%02X " , sep, addrBytes[ndx] ) );
            }
        }
        return addrBuff.toString();
    }

    void dumpIpConfigMacs() {
        String cmd =  " ipconfig /all " ;
        BufferedReader reader = null;
        try {
            Process proc = Runtime.getRuntime().exec( cmd );
            StringBuilder outBuff = new StringBuilder();
            proc.waitFor();
            reader = new BufferedReader( new InputStreamReader( proc.getInputStream() ) );
            ArrayList<String> addrsList = new ArrayList<String>();
            String nextLine = null;
            do {
                nextLine = reader.readLine();
                if ( nextLine != null ) {
                    if ( nextLine.contains(  " Physical Address "  ) ) {
                        String[] parts = nextLine.split(  " : "  );
                        addrsList.add( parts[1].trim() );
                    }
                }
            } while ( nextLine != null );
            dumpMacs( addrsList,  " ipconfig "  );
        } catch ( Exception e ) {
            System.err.println(  " can't execute  "  + cmd +  " :  "  + e );
        } finally {
            if ( reader != null ) {
                try {
                    reader.close();
                } catch ( Exception e ) {
                    System.err.println(  " can't execute  "  + cmd +  " :  "  + e );
                }
            }
        }
    }

    void dumpMacs(List<String> inMacs, String inSource) {
        System.err.println(  "  "  );
        System.err.println(  " MAC addresses derived from  "  + inSource +  " : "  );
        for ( String nextMac : inMacs ) {
            System.err.println(  "     "   + nextMac );
        }
    }
}
---------- END SOURCE ----------
Comments
Duplicate of JDK-8021372
01-12-2022

MAC addresses derived from ipconfig : C:\Users\chris\work>ipconfig /all | find "Physical" Physical Address. . . . . . . . . : 00-05-9A-3C-7A-00 Physical Address. . . . . . . . . : A0-88-B4-88-BA-8D Physical Address. . . . . . . . . : D0-DF-9A-3D-3C-17 Physical Address. . . . . . . . . : A0-88-B4-88-BA-8C Physical Address. . . . . . . . . : 5C-26-0A-83-6A-5C MAC addresses derived from NetworkInterface : C:\Users\chris\work>"C:\Program Files (x86)\Java\jdk1.7.0_04\bin\java" NetInterfaceBug 5C - 26 - 0A - 83 - 6A - 5C 5C - 26 - 0A - 83 - 6A - 5C 5C - 26 - 0A - 83 - 6A - 5C 5C - 26 - 0A - 83 - 6A - 5C 5C - 26 - 0A - 83 - 6A - 5C This clearly shows the issue.
08-08-2013

This issue is reproducible on previous 7 updates, 7u4 and 7u11 for example.
26-07-2013