Nicolai
Nicolai

Reputation: 3728

Why does java NetworkInterface.getHardwareAddress return empty byte array on Windows?

I have the following code on a Windows machine:

for(Enumeration enm = NetworkInterface.getNetworkInterfaces(); enm.hasMoreElements();){
  NetworkInterface network = (NetworkInterface) enm.nextElement();
    if(null != network.getHardwareAddress()){
      return EthernetAddress.valueOf(network.getHardwareAddress());
    }
 }

This fails because the network.getHardwareAddress() returns an empty byte array, instead of null as stated in the javadocs for NetworkInterface. Does anyone know why this may happen?

Upvotes: 0

Views: 4362

Answers (2)

Yogesh Seralia
Yogesh Seralia

Reputation: 350

The byte array returned from network.getHardwareAddress() gets null if you are not connected to any network (LAN or WAN) hence it returns null value

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533492

I did this and saw that the loopback (which apeared first) has no mac address.

My guess is that null is intended for when the OS returns that the MAC was unavailable. However it may have returned empty data instead.

On Linux, it doesn't show me the loopback.

Upvotes: 2

Related Questions