Common Java tools-obtain the MAC address of the NIC in use on the local machine

Source: Internet
Author: User

Java NativeCode, Through java.net. networkinterface is used to obtain the first network card in use on the local machine. It is common to all platforms. It is normal to test winows and SuSE Linux (if you want to obtain all the network card MAC addresses-use ISUP () to identify whether the network interface is running, or to obtain the MAC addresses of all network adapters in use, make a slight change:

Import java.net. networkinterface; import Java. util. enumeration;/*** get MAC address tool class */public class macutil {private macutil () {}/ *** in XX-XX-XX-XX-XX-XX format, obtain the MAC address * @ return * @ throws exception */public static string getmacaddress () throws exception {enumeration <networkinterface> Ni = networkinterface. getnetworkinterfaces (); While (Ni. hasmoreelements () {networkinterface neti = ni. nextelement (); byte [] B Ytes = neti. gethardwareaddress (); If (NETI. ISUP () & neti! = NULL & bytes! = NULL & bytes. length = 6) {stringbuffer sb = new stringbuffer (); For (byte B: bytes) {// bitwise AND operation with 11110000 to read the Current byte 4-bit sb. append (integer. tohexstring (B & 240)> 4); // bitwise AND operation with 00001111 to read the Current byte 4-bit sb. append (integer. tohexstring (B & 15); sb. append ("-");} sb. deletecharat (sb. length ()-1); return sb. tostring (). touppercase () ;}} return NULL;} public static void main (string [] ARGs) throws exception {system. out. println (macutil. getmacaddress ());}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.