Get mac address in Java using getHardwareAddress not deterministic

I had a problem getting the MAC address of the machine, which was resolved in this issue using the following code:

Process p = Runtime.getRuntime().exec("getmac /fo csv /nh"); java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(p.getInputStream())); String line; line = in.readLine(); String[] result = line.split(","); System.out.println(result[0].replace('"', ' ').trim()); 

However, I would like to know why this code does not work. Each time it reads the MAC address, it returns a different value. At first I thought it was because getHash, perhaps using a timestamp that I don't know ... But even deleting it, the result changes.

the code

  public static byte[] getMacAddress() { try { Enumeration<NetworkInterface> nwInterface = NetworkInterface.getNetworkInterfaces(); while (nwInterface.hasMoreElements()) { NetworkInterface nis = nwInterface.nextElement(); if (nis != null) { byte[] mac = nis.getHardwareAddress(); if (mac != null) { /* * Extract each array of mac address and generate a * hashCode for it */ return mac;//.hashCode(); } else { Logger.getLogger(Utils.class.getName()).log(Level.WARNING, "Address doesn't exist or is not accessible"); } } else { Logger.getLogger(Utils.class.getName()).log(Level.WARNING, "Network Interface for the specified address is not found."); } return null; } } catch (SocketException ex) { Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex); } return null; } } 

Example output (I print directly from an array of bytes, but it is enough to see that I think different)

 [ B@91cee [ B@95c083 [ B@99681b [ B@a61164 [ B@af8358 [ B@b61fd1 [ B@bb7465 [ B@bfc8e0 [ B@c2ff5 [ B@c8f6f8 [ B@d251a3 [ B@d6c16c [ B@e2dae9 [ B@ef5502 [ B@f7f540 [ B@f99ff5 [ B@fec107 

Thanks in advance

+7
source share
4 answers

B@91cee This is actually the result of the toString() the byte[] array method.

I would suggest you print the value using new String(mac) instead.

byte[].toString() is implemented as:

 public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashCode()); } 

Since, by default, Object.hashCode() is implemented as an address in memory, so it is not sequential, since you create a new Object each time.

Edit:

Since the returned byte is in hexadecimal, you must convert it to a decimal string. The code can be seen from here.

+9
source

Here is an example from the Mkyong.com website on how to get the MAC address in Java:

 package com.mkyong; 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(); } } } 
+5
source

The answer from the Spaniard does not work if the machine is not connected, and it will give different values ​​depending on the network to which you are connected.

it does not depend on any ip address:

 public class MacAdress { public static void main(String[] args) { try { InetAddress ip = InetAddress.getLocalHost(); System.out.println("Current IP address : " + ip.getHostAddress()); Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces(); while(networks.hasMoreElements()) { NetworkInterface network = networks.nextElement(); byte[] mac = network.getHardwareAddress(); if (mac != null) { 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) ? "-" : "")); } } } } catch (UnknownHostException e) { e.printStackTrace(); } catch (SocketException e){ e.printStackTrace(); } } } 
+4
source
 public static String getHardwareAddress() throws Exception { InetAddress ip = InetAddress.getLocalHost(); NetworkInterface ni = NetworkInterface.getByInetAddress(ip); if (!ni.isVirtual() && !ni.isLoopback() && !ni.isPointToPoint() && ni.isUp()) { final byte[] bb = ni.getHardwareAddress(); return IntStream.generate(ByteBuffer.wrap(bb)::get).limit(bb.length) .mapToObj(b -> String.format("%02X", (byte)b)) .collect(Collectors.joining("-")); } return null; } 
+2
source

All Articles