Package com.yjm.testmac;import java.net.inetaddress;import java.net.networkinterface;import java.net.SocketException;import java.net.UnknownHostException;import java.util.Enumeration; Public class mactest {public static void main (String[] args) {// Get the MAC address information of the machine test ();// the network card information Test1 () based on the current IP address information object. Private static void test1 () {system.out.println ("Run start in ==========test1 method ..."); try {inetaddress ipinfo = inetaddress.getlocalhost ();// output IP address content System.out.println ( Ipinfo.gethostaddress ());// obtains the NIC content byte array byte[] byaddress = networkinterface.getbyinetaddress (Ipinfo). gethardwareaddress ();// output array length System.out.println (byaddress.length);// address string container StringBuffer stringbuffer = new stringbuffer ();// a single array value to convert for (int i = 0; i < byaddress.length; i++) {// Single array value is a groundAddress number Add delimiter if (i != 0) {stringbuffer.append ("-");} bytes converted to 16 decimal int number = byaddress[i] & 0xff; string straddr = integer.tohexstring (number); System.out.println ("Converted: =======================================" + straddr);// when the value of the conversion is single digit , in front of the complement 0if (Straddr.length () == 1) {stringbuffer.append ("0" + straddr);} else {stringbuffer.append (STRADDR);}} Output converted MAC address System.out.println ("converted MACAddress:" + stringbuffer.tostring (). toUpperCase ());} catch (unknownhostexception e) {e.printstacktrace ();} catch (socketexception e) {e.printstacktrace ();} System.out.println ("Run over in ==========test1 method ..."); Private static void test () {system.out.println ("Run start in ==========test method ...");// Initialize NIC information container enumeration<networkinterface> allnetdevices = null;// Initializes a Nic object networkinterface networkinterface = null;// wait until the network card byte array information byte[] networkbyte = null;// Package NIC information string stringbuffer stringbuffer=null;try {// get NIC information container allnetdevices = Networkinterface.getnetworkinterfaces ();while (Allnetdevices.hasmoreelements ()) {// get a single-card Nic object Iterative output NIC information networkinterface = allnetdevices.nextelement ();// get network card information that is being running if (Networkinterface.isup ()) {//the network adapter address container Stringbuffer = new stringbuffer () used after converting bytes;// Nic MAC address byte container networkbyte = networkinterface.gethardwareaddress ();//NIC name System.out.println ("NIC Name: "+networkinterface.getname ()); //Nic Information System.out.println ("NIC info : " + Networkinterface.getdisplayname ());// a single array value for conversion for (int i = 0; i < networkbyte.length; i++) {// Single array value is an address bit number add delimiter if (i != 0) {stringbuffer.append ("-");} bytes converted to 16 decimal int number = networkbyte[i] & 0xff; string straddr = integer.tohexstring (number); System.out.println ("Converted: ==================================" + straddr);// when the value of the conversion is single digit, In front of the complement 0if (Straddr.length () == 1) {stringbuffer.append ("0" + straddr);} else {stringbuffer.append (STRADDR);}} Output converted MAC address System.out.println ("Converted MACAddress is:" + stringbuffer.tostring (). toUpperCase ());}}} catch (socketexception e) {e.printstacktrace ();} System.out.println ("Run over in ==========test method ...");}
Method can only get 127.0.0.1 under Linux, the correct IP address can only be obtained under Windows.
Output Result:
Run start in ==========test method ...
Network card name: Lo
Nic information: Software Loopback Interface 1
The converted MACAddress are:
Nic Name: Eth3
Nic Information: Realtek PCIe GBE Family Controller
After conversion: ==================================14
After conversion: ==================================da
After conversion: ==================================E9
After conversion: ==================================2B
After conversion: ==================================38
After conversion: ==================================10
The converted MACAddress is: 14-da-e9-2b-38-10
Nic Name: Net4
Nic Information: Teredo Tunneling Pseudo-Interface
After conversion: ==================================0
After conversion: ==================================0
After conversion: ==================================0
After conversion: ==================================0
After conversion: ==================================0
After conversion: ==================================0
After conversion: ==================================0
After conversion: ==================================E0
The converted MACAddress is: 00-00-00-00-00-00-00-E0
Run over in the ==========test method ...
Run start in ==========test1 method ...
192.168.101.243
6
After conversion: =======================================14
After conversion: =======================================da
After conversion: =======================================E9
After conversion: =======================================2B
After conversion: =======================================38
After conversion: =======================================10
The converted MACAddress is: 14-da-e9-2b-38-10
Run over in the ==========test1 method ...
Java gets the current computer card MAC address