0

Possible Duplicate:
How to get a unique computer identifier in Java (like disk id or motherboard id)
Need to identify local machine somehow in java

I want to get some sort of unique ID for the computer (ie IP address).

I have tried:

InetAddress inetAddress = null;
try {
    inetAddress = InetAddress.getLocalHost();
} catch (final UnknownHostException ex) {}
return inetAddress.getHostAddress();

This gives an IP, but this one seems to change periodically. Is there a way to get one the DOES NOT change?

Thanks in advance!

NOTE:

I need to be able to associate this ID with the computer.

Community
  • 1
  • 1
Dylan Wheeler
  • 6,740
  • 14
  • 55
  • 79

1 Answers1

2

If the machine is on DHCP, the IP address will change periodically. You could use the hostname, or you could use the MAC address, which you can get with java.net.NetworkInterface.getHardwareAddress()

Martin Broadhurst
  • 8,983
  • 2
  • 27
  • 34
  • Will this address NOT change? – Dylan Wheeler Jan 25 '12 at 23:31
  • 1
    Yep the MAC address is clearly the best choice here. Ignoring some rare situations (say VMs, someone changing the MAC address) it's guaranteed to be globally unique - what else could you want. Although it still can change - we could swap the network card or install a second one, etc. – Voo Jan 25 '12 at 23:31
  • Could I get help with adding code for this? I can't seem to get it right. – Dylan Wheeler Jan 25 '12 at 23:37
  • 1
    How hard have you tried in 5 minutes? :-) – Martin Broadhurst Jan 25 '12 at 23:42
  • It keeps on saying: "non-static method getHardwareAddress() cannot be referenced from a static context." No matter what I do, I can't get it. Happy? – Dylan Wheeler Jan 25 '12 at 23:44
  • @JavaCoder-1337: That's an _instance method_. Note that most users have multiple `NetworkInterface`s, and that WiFi adapters on laptops can be turned off. – SLaks Jan 25 '12 at 23:46
  • If you know the IP address you want to use, call `NetworkInterface.getByInetAddress()` with it to get hold of a `NetworkInterface`. Then call `getHardwareAddress()` on that. – Martin Broadhurst Jan 25 '12 at 23:51