Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6709430

Unable to detect MAC address in Windows Vista

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 6
    • core-libs
    • x86
    • windows_vista

      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 ----------

            michaelm Michael McMahon
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: