from: http://www.mkyong.com/java/how-to-get-mac-address-in-java/
Since JDK 1.6, Java developers are able to access network card detail via NetworkInterface
class. In this example, we show you how to get the localhost MAC address in Java.
App.java – Get MAC Address via NetworkInterface.getByInetAddress()
import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; public class App{ public static void main(String[] args){ InetAddress ip; try { ip = InetAddress.getLocalHost(); System.out.println("Current IP address : " + ip.getHostAddress()); NetworkInterface network = NetworkInterface.getByInetAddress(ip); byte[] mac = network.getHardwareAddress(); System.out.print("Current MAC address : "); StringBuilder sb = new StringBuilder(); for (int i = 0; i < mac.length; i++) { sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "")); } System.out.println(sb.toString()); } catch (UnknownHostException e) { e.printStackTrace(); } catch (SocketException e){ e.printStackTrace(); } } }
Output
Current IP address : 192.168.1.22 Current MAC address : 00-26-B9-9B-61-BF
Note
This NetworkInterfaceNetworkInterface.getHardwareAddress() method is only allowed to access localhost MAC address, not remote host MAC address.
This NetworkInterfaceNetworkInterface.getHardwareAddress() method is only allowed to access localhost MAC address, not remote host MAC address.
Old day...
Before JDK1.6 is released, many are using the command and pattern to get the MAC address in Windows, minor code changes will enable it to get the MAC address in *nux as well.
App.java - Get MAC Address via command & pattern
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.regex.Matcher; import java.util.regex.Pattern; public class App{ public static void main(String[] args) throws IOException{ String command = "ipconfig /all"; Process p = Runtime.getRuntime().exec(command); BufferedReader inn = new BufferedReader(new InputStreamReader(p.getInputStream())); Pattern pattern = Pattern.compile(".*Physical Addres.*: (.*)"); while (true) { String line = inn.readLine(); if (line == null) break; Matcher mm = pattern.matcher(line); if (mm.matches()) { System.out.println(mm.group(1)); } } } }
Output
02-00-4E-43-50-49 90-4C-E5-44-B9-8F 00-26-B9-9B-61-BF 00-00-00-00-00-00-00-E0 00-00-00-00-00-00-00-E0 00-00-00-00-00-00-00-E0 00-00-00-00-00-00-00-E0 00-00-00-00-00-00-00-E0
This obsolete method is not really efficient, because it does not display which MAC address is using now, what it did is just print out all the available MAC address currently attached. However, it's nice to share here.
相关推荐
Unit_3_How_do_you_get_to_school课件.ppt
Writing_Science_How_to_Write_Papers_That_Get_Cited_and_Proposals_That_Get_Funded
Java_How_to_Program_9th_Edition.pdf, good book for JAVA.
最新经典入门好书 Java_How_to_program_10th 亚马逊5星推荐,高清完整,详尽,包含JAVA 8,1245页。一本完整学习JAVA的经典入门好书。强烈推荐。
信息安全_数据安全_How to Get Promoted:Developing M 安全研究 漏洞分析 安全审计 安全验证 风控系统
「工控安全」How_to_Get_Promoted:Developing_Metrics_to_Show_How_Threat_Intel_Works - 安全防御 网站安全 金融安全 系统安全 NGFW 勒索病毒
IEEE 期刊 latex格式说明 官方教程, 有详细格式指导,latex教程,英文原版,发英文论文必备手册
Java_How-to-Program_10ed 英文电子版 有需要者自取。
KBA_180523214236_2__PMIC__PMI632_How_to_configure_.pdf
Think Java: How to Think Like a Computer Scientist by Allen B. Downey, Chris Mayfield 2016 | ISBN: 1491929561 Currently used at many colleges, universities, and high schools, this hands-on ...
RTEMS的bsp_howto文档的原创翻译。
C# 原版材料 CSharp_How_to_Program
Java开发XML应用
如何关闭QC3.0
A very useful tutorial on how to program XML using Java.
the document describes how to build a cascade classifier for object detection with haar features
Deitel-C_ How to Program
2014pep六年级上册英语unit_1_How_can_I_get_there_1.ppt
OpenCV_ObjectDetection_HowTo.rar