How to get cpu-id in java?

So you want a unique number (or string?) that identifies the user's computer? Or at least unique enough that the chance of a duplicate is very low, right?

You can get the Mac address of the network interface. This is making many assumptions, but it may be good enough for your needs:

final byte[] address = NetworkInterface.getNetworkInterfaces().nextElement().getHardwareAddress();
System.out.println("address = " + Arrays.toString(address));

This gives you an array of bytes. You can convert that to an id in several ways... like as a hex string.

Expect support though, when people replace bits of hardware in their computer.


I think such OS specific command is not available in Java.

This link shows a way to run it on windows.


You can't (reliably) get hardware information in pure Java. You would have to use JNA or JNI. Can you clarify what kind of encryption system you're building, and why you need the hardware info?

EDIT: Steve McLeod has noted that Java has a NetworkInterface.getHardwareAddress() method. However, there are serious caveats, including the fact that not all Java implementations allow access to it, and MAC addresses can be trivially forged.