views:

2327

answers:

7

Hi, I'd like to get an id unique to a computer with Java, on Windows, MacOS and if possible, linux. It could be a disk UUID, motherboard s/n... Runtime.getRuntime().exec can be used (it is not an applet). Ideas ?

+4  A: 

It is common to use the MAC address is associated with the network card.

The address is available in Java 6 through through the following API:

Java 6 Docs for Hardware Address

I haven't used it in Java, but for other network identification applications it has been helpful.

nickk_can
I've think about it, but when the network card isn't connected, i can't get any MAC address
Gohu
It's also possible for the user to switch network cards. On some laptops, when running off battery, the (wired) Ethernet card is disabled to save battery power, and is thus not visible to the operating system.
Steve Kuo
+4  A: 

What do you want to do with this unique ID? Maybe you can do what you want without this ID.

The MAC address maybe is one option but this is not an trusted unique ID because the user can change the MAC address of a computer.

To get the motherboard or processor ID check on this link.

Pedro Ghilardi
+2  A: 

I think you should look at this link ... you can make a mixed key using several identifiers such as mac+os+hostname+cpu id+motherboard serial number.

Michel Kogan
Links which you placed behind cpuid/moboserial describes Windows-specific ways. This isn't crossplatform.
BalusC
in linux you can get hdd serial number using this command : hdparm -i /dev/sda1 | awk '/SerialNo=/{print $NF}' ( just recognize OS and try different methods ) you can find MB serial number using lshw command
Michel Kogan
+2  A: 

On Windows only, you can get the motherboard ID using WMI, through a COM bridge such as JACOB.

Example:

import java.util.Enumeration;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.EnumVariant;
import com.jacob.com.Variant;

public class Test {
    public static void main(String[] args) {
        ComThread.InitMTA();
        try {
            ActiveXComponent wmi = new ActiveXComponent("winmgmts:\\\\.");
            Variant instances = wmi.invoke("InstancesOf", "Win32_BaseBoard");
            Enumeration<Variant> en = new EnumVariant(instances.getDispatch());
            while (en.hasMoreElements())
            {
                ActiveXComponent bb = new ActiveXComponent(en.nextElement().getDispatch());
                System.out.println(bb.getPropertyAsString("SerialNumber"));
                break;
            }
        } finally {
            ComThread.Release();
        }
    }
}

And if you choose to use the MAC address to identify the machine, you can use WMI to determine whether an interface is connected via USB (if you want to exclude USB adapters.)

It's also possible to get a hard drive ID via WMI but this is unreliable.

finnw
+1  A: 

You find examples for UUID generation as well as Java MAC adress lookup at here: http://johannburkard.de/software/uuid/

stacker
Java UUID Generator @ http://jug.safehaus.org/ uses JNI to get the MAC address avoiding the need to issue a system call
bmatthews68
Additional information in this Q: http://stackoverflow.com/questions/192920/generating-a-globally-unique-identifier-in-java
Clinton
+1  A: 

Be careful when using the MAC address as an identifier. I've experienced several gotchas:

  1. On OS X, ethernet ports that are not active/up do not show up in the NetworkInterface.getNetworkInterfaces() Enumeration.
  2. It's insanely easy to change a MAC address on cards if you've got appropriate OS privileges.
  3. Java has a habit of not correctly identifying "virtual" interfaces. Even using the NetworkInterface.isVirtual() won't always tell you the truth.

Even with the above issues, I still think it's the best pure Java approach to hardware locking a license.

Jason Nichols
+1  A: 

Not Knowing all of your requirements. For example, are you trying to uniquely identify a computer from all of the computers in the world, or are you just trying to uniquely identify a computer from a set of users of your application. Also, can you create files on the system?

If you are able to create a file. You could create a file and use the creation time of the file as your unique id. If you create it in user space then it would uniquely identify a user of your application on a particular machine. If you created it somewhere global then it could uniquely identify the machine.

Again, as most things, How fast is fast enough.. or in this case, how unique is unique enough.

sindri