프로그래밍/java

자바로 MAC Address 알아오기

Super User 2011. 1. 14. 10:08


import java.net.*;
public class NetworkTest {
    
    public static void main(String[] args) throws Exception  {
        
  InetAddress address = InetAddress.getLocalHost();

  NetworkInterface ni = NetworkInterface.getByInetAddress(address);

  byte[] mac = ni.getHardwareAddress();
  
  String macaddr = "";
  for (int i = 0; i < mac.length; i++) {
   macaddr += String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
  }
  System.out.println (macaddr);
    }
}