Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to get mac address

 
lynn fann
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can i get the local machine's MAC address?
 
Scott Johnson
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're running JSE6:

NetworkInterface.getHardwareAddress()

If you're running an earlier JVM, there is no pure Java solution. You could do something like this on Linux:

Runtime.getRuntime().exec("/sbin/ifconfig");

and parse the output.

or on Windows:

Runtime.getRuntime().exec("cmd.exe -c ipconfig -all");
 
lynn fann
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am working on windows and my jdk is 1.4. If i would llike to use JNI, how should i go about it.

I have try the code NetworkInterface.getHardwareAddress(), but it return me an error saying cannot resolve symbol, getHardwareAddress()


if i using this code:
Runtime.getRuntime().exec("cmd.exe -c ipconfig -all");

how to get the MAC address. I have done a system.out.print for the statement, i.e. System.out.println(Runtime.getRuntime().exec("cmd.exe -c ipconfig -all").toString());

i get the result as "java.lang.Win32Process@194df86"


[ December 19, 2006: Message edited by: lynn fann ]
[ December 19, 2006: Message edited by: lynn fann ]
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Runtime.exec(String) method returns a java.lang.Process object. Use that object to get an InputStream and obtain console results.

For instance:



I hope that helps!
 
lynn fann
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Edwin,

thanks, it helps.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic