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 ());}}