-2

I have a chunk of code that returns the mac address of a PC very correctly, but that's only when there is an internet access, but I need it offline in a project I'm carrying out. If not possible, is there any other possible way of uniquely identifying a PC?

1 Answers1

2

You certainly use an indirection based on the IP address, in your code snippet. This may explain why you do not get anything when Internet network access is down.

Here is a code snippet that does not depend on the network connection status. It displays each MAC address of your PC. Note that a PC often has multiple MAC addresses. Each address will be displayed by this code snippet.

package com.stackoverflow;

import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class GetHWAddresses {
    public static void main(String[] args) throws SocketException {
    final Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
        while (e.hasMoreElements()) {
            final byte [] mac = e.nextElement().getHardwareAddress();
            if (mac != null) {
                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());
            }
        }
    }
}
Alexandre Fenyo
  • 4,526
  • 1
  • 17
  • 24