My (mostly technical) blog

Get the MAC address using Java on Windows

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

Advertisements

3 Responses to "Get the MAC address using Java on Windows"

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

Hi, it was a really good article! I am trying to implement something like this, but it’s going to be in a web page. What I’m suppose to do is to identify the mac address of the client. The problem is that, when I tried to use the method you mentioned here, the following exception appears: java.io.IOException: ipconfig: not found Obviously it is not identifying the client pc, is there another way to make it work? By the way, I can’t use JDK 1.6 so there’s no possibility to use the method of getHardwareAddress(). Any other suggestion?

Thanks in advance, for any help!

Can we use this code in Mac ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Ahmed Sabbour's Facebook profile
May 2007
S M T W T F S
« Apr   Nov »
 12345
6789101112
13141516171819
20212223242526
2728293031  

Tweets

Error: Twitter did not respond. Please wait a few minutes and refresh this page.

RSS StackOverflow

Recently bookmarked

%d bloggers like this: