My (mostly technical) blog

Get the MAC address using Java on Windows

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();
}

1 Response to "Get the MAC address using Java on Windows"

Thanks a lot. It’s a easiest solution i could find after lots of searching.

Leave a Reply

Ahmed Sabbour's Facebook profile

 

May 2007
S M T W T F S
« Apr   Nov »
 12345
6789101112
13141516171819
20212223242526
2728293031  

Recently bookmarked