Unique Host ID

I'm looking for a cross-platform way for hosts to self-generate an id which will (with medium/high certainty) be unique on the Internet and consistent between program executions without storing to disk.

The ideal solution would be to use a MAC address from one of the system's network devices, but I can't find a clean platform-neutral way of finding these,

If there is a solution which will survive restarting the JVM but not a system reboot, that would work also.

[487 byte] By [lannma] at [2007-11-27 9:00:47]
# 1
Why not use the IP address?
prometheuzza at 2007-7-12 21:29:53 > top of Java-index,Java Essentials,Java Programming...
# 2

That is my backup strategy, but if I do that I have to make sure its an external IP, as the system could be NATed and lots of NATed hosts have similar IPs (e.g. 192.168.1.x).

That would mean a call to an outside service that provides external IP lookup, which means hard-coding a site that could go down.

lannma at 2007-7-12 21:29:53 > top of Java-index,Java Essentials,Java Programming...
# 3
If you use Java 1.6, you could make use of this method: http://java.sun.com/javase/6/docs/api/java/net/NetworkInterface.html#getHardwareAddress()
prometheuzza at 2007-7-12 21:29:53 > top of Java-index,Java Essentials,Java Programming...
# 4
Thanks, that's very helpful. Can I write code that will behave differently depending on the JRE version?Something like C (except at runtime):#ifdef JAVA_6getHardwareAddress()#elsegetExternalIp()#endifMessage was edited by:
lannma at 2007-7-12 21:29:53 > top of Java-index,Java Essentials,Java Programming...
# 5

It's a bit of a hack:

if(System.getProperty("java.vm.version").startsWith("1.6")) {

System.out.println("Found 1.6!");

} else {

System.out.println("Found something old...");

}

prometheuzza at 2007-7-12 21:29:53 > top of Java-index,Java Essentials,Java Programming...
# 6

I think this should work:

try {

unique = if.getHardwareAddress();

} catch (NoSuchMethodException e) {

unique = getExternalIp();

}

lannma at 2007-7-12 21:29:53 > top of Java-index,Java Essentials,Java Programming...
# 7
There's also java.util.UUID.
jverda at 2007-7-12 21:29:53 > top of Java-index,Java Essentials,Java Programming...