Posted by: sabbour on: May 10, 2007
Update: In JDK 1.6, you can get the MAC address using this method http://java.sun.com/javase/6/docs/api/java/net/NetworkInterface.html#getHardwareAddress()
A friend of mine ran across the problem of finding his MAC address so that he could use it in his project. Looking around the net we found a command line on Windows called “getmac”. This command can get the MAC addresses of all the interfaces that are connected to your computer.
Below is a simple Java method that can retrieve the MAC address as a String. The only limitation is that it assumes that you only have 1 network interface (or more specifically, it will always return the MAC address of the first network interface). You could easily modify it to work for you
public static String getMac() throws java.io.IOException {
Process p = Runtime.getRuntime().exec(“getmac /fo csv /nh”);
java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(p.getInputStream()));
String line;
line = in.readLine();
String[] result = line.split(“,”);
return result[0].replace(‘”‘, ‘ ‘).trim();
}
July 3, 2009 at 9:15 am
Thanks a lot. It’s a easiest solution i could find after lots of searching.