Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 19th, 2012  |  syntax: None  |  size: 0.60 KB  |  hits: 3  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public class ListMACsJava {
  2.   public static void main(String[] args) {
  3.     try {
  4.           Enumeration<NetworkInterface> nics =
  5.                           NetworkInterface.getNetworkInterfaces();
  6.           while (nics.hasMoreElements()) {
  7.             NetworkInterface nic = nics.nextElement();
  8.             byte[] mac = nic.getHardwareAddress();
  9.             String macStr = "";
  10.             if (mac != null)
  11.               for (int i = 0; i < mac.length; i++) {
  12.                 macStr += String.format("%02x", mac[i]);
  13.                 if (i < mac.length - 1)
  14.                   macStr += ":";
  15.                 }
  16.             System.out.println(macStr);
  17.            }
  18.         } catch (SocketException e) {
  19.           System.err.println("Ooops!");
  20.         }
  21.   }
  22. }