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 

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 {
Process p = Runtime.getRuntime().exec(
“getmac /fo csv /nh”); in =
String line;
line = in.readLine();
String[] result = line.split(
return result[0].replace(‘”‘, ‘ ‘).trim();


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: 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: Logo

You are commenting using your 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
« Apr   Nov »


RSS StackOverflow

Recently bookmarked

%d bloggers like this: