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
「工控安全」How_to_Get_Promoted:Developing_Metrics_to_Show_How_Threat_Intel_Works - 安全防御 网站安全 金融安全 系统安全 NGFW 勒索病毒
### Java_How_to_Program_9th_Edition.pdf 关键知识点概述 #### 一、Deitel & Associates, Inc. 公司介绍 Deitel & Associates, Inc. 是一家国际知名的作者与企业培训组织,专注于软件开发和技术教育领域。该公司...
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 ...
"XML How to Program - Java.zip"可能是一个包含关于如何使用Java处理XML的教程或参考材料。 Java API for XML Processing (JAXP) 是Java平台的标准部分,它提供了处理XML的基本工具。JAXP允许开发者进行XML解析、...
1. 书籍内容概述:《Writing Science: How to Write Papers That Get Cited and Proposals That Get Funded》是一本专注于科学研究写作的指南书,由Joshua Schimel所著。本书旨在指导科研人员如何撰写能够吸引关注和...
the document describes how to build a cascade classifier for object detection with haar features
Java的`java.io.ObjectOutputStream`和`java.io.ObjectInputStream`可以配合`java.io.Serializable`接口实现对象的序列化和反序列化,而`javax.xml.bind.JAXBContext`和`javax.xml.bind.Marshaller`等类可以实现XML...
包含了将近100张图表,这些图表能用来根据载流能力和温度限制条件来精确估计导线的宽度,这个Excel工具允许设计者根据不同的叠层结构和设计条件填入不同的数值来计算导线宽度。很简单的在绿色单元格内输入项目的设计...
VC编程实现如何获得INI文件的全部段名经典代码VC Programming how to get all the INI file section name classic code
baochunli_howto_write_paper.pdf
linux_howto-cn<br>中文版 Linux HOWTO
How to Get Ideas - Jack Foster.pdf
R_package_demonstrating_how_to_use_shinyBootstrap2_shinyBS2demo
本站提供的主要课程,总共 7 个阶段,69个模块,共 1476 个知识页面!包含:Java基础、Java中级、Java高级、Java应用、Java框架、Java工具及中间件、分布式和集群、数据库、第三方开发、实践项目、面试题!
2013年春新版七年级英语下册Unit3_How_do_you_get_to_school_Self_check_加复习课件