Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Java unique identifier

  1. #1
    Join Date
    Jun 2011
    Beans
    16
    Distro
    Ubuntu 11.04 Natty Narwhal

    Java unique identifier

    Hi all, I was wondering if there was a way for java to get a unique identifying string from any computer. I was thinking about doing this:

    String UUID = (new NetworkInterface).toString();

    I was wondering how often this might change on a computer (Going to use it for software licenses) and if there is a better way to do this.

    BTW, I am going to hash the UUID later in the program...

    Edit1: Didn't test the code, might not compile (just a brainstorm)

  2. #2
    Join Date
    Apr 2006
    Location
    New Jersey, US
    Beans
    98
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Re: Java unique identifier

    A good unique identifier is the MAC address and it does not change often, but it can if someone really wanted to change it.

  3. #3
    Join Date
    May 2009
    Beans
    525

    Re: Java unique identifier

    Java has a UUID class. Maybe it will meet your needs.

    http://download.oracle.com/javase/6/...util/UUID.html

  4. #4
    Join Date
    Dec 2007
    Location
    The last place I look
    Beans
    Hidden!
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Java unique identifier

    well, the first question is how do you define "Computer"?
    the NIC UUID is probably based on OS install, and thus may not be completely globally unique, and it does change across rebuilds, and perhaps even kernel installs.

    Intel toyed with making a universal CPU identifier that would allow a motherboard/cpu combo to be uniquely id'd but there was huge outcry and they backed off. http://bigbrotherinside.org/

    here is how MS defines a computer for activation:
    http://technet.microsoft.com/en-us/l.../bb457054.aspx
    Things are rarely just crazy enough to work, but they're frequently just crazy enough to fail hilariously.

  5. #5
    Join Date
    Jun 2011
    Beans
    16
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: Java unique identifier

    Ok, Thank you for your suggestions, but I am having trouble with this piece of code.
    Code:
        String getUUID(){
        	String UUID = "No_Mac_Addresss";
        	try {
        		NetworkInterface inter = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
    		UUID = new String(inter.getHardwareAddress()); //NPE here
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    		
        	return UUID;
        }
    I am receiving a NullPointerException where the //NPE here is

    Any thoughts as what that might be caused by?

    If it matters, I am on ubuntu 11.04 using the Sun java.
    Last edited by tyzoid; July 21st, 2011 at 06:39 PM.

  6. #6
    Join Date
    Dec 2007
    Location
    The last place I look
    Beans
    Hidden!
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Java unique identifier

    Quote Originally Posted by tyzoid View Post
    Ok, Thank you for your suggestions, but I am having trouble with this piece of code.
    Code:
        String getUUID(){
            String UUID = "No_Mac_Addresss";
            try {
                NetworkInterface inter = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
            UUID = new String(inter.getHardwareAddress()); //NPE here
        } catch (Exception e) {
            e.printStackTrace();
        }
            
            return UUID;
        }
    I am receiving a NullPointerException where the //NPE here is

    Any thoughts as what that might be caused by?

    If it matters, I am on ubuntu 11.04 using the Sun java.

    well, looks like either 'inter' or 'getHardwareAddress' is\returns null.
    add an ifstatement to check that both exist before attempting to do anything with them that would trigger a nullref ex.
    Things are rarely just crazy enough to work, but they're frequently just crazy enough to fail hilariously.

  7. #7
    Join Date
    Jun 2011
    Beans
    16
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: Java unique identifier

    inter is null.

    Here is the current code:

    Code:
        String getUUID(){
        	String UUID = "No_Mac_Addresss";
        	try {
        		InetAddress addr = InetAddress.getLocalHost();
        		System.out.println(addr.getHostAddress());
        		NetworkInterface inter = NetworkInterface.getByInetAddress(addr);
        		if(inter == null){System.out.println("null");} else {System.out.println("not null");}
        		if(inter.isUp()){System.out.println("true");} else {System.out.println("false");} //NPE here
        		byte[] test = inter.getHardwareAddress();
    		UUID = new String(test);
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    		
        	return UUID;
        }
    Here is the output:
    Code:
    127.0.1.1
    null
    java.lang.NullPointerException
    	at ***.***.***.***.***.getUUID(***.java:**)
    I am getting the NPE where it is labled //NPE here

    Can anyone think of a solution?
    Last edited by tyzoid; July 22nd, 2011 at 07:16 PM.

  8. #8
    Join Date
    Dec 2007
    Location
    The last place I look
    Beans
    Hidden!
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Java unique identifier

    well, don't run this line if inter is null:
    Code:
    byte[] test = inter.getHardwareAddress();
    if inter is null, the method will never ever return anyway, and any attempt to access it will cause an Nullrefex.
    you can:

    • attempt to get the info by other means, or
    • supply a safe default value for cases where the info is inaccessible, or
    • throw the exception to the caller and leave remediation up to them
    Things are rarely just crazy enough to work, but they're frequently just crazy enough to fail hilariously.

  9. #9
    Join Date
    Dec 2007
    Location
    The last place I look
    Beans
    Hidden!
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Java unique identifier

    ok, this works for me:
    Code:
    import java.net.InetAddress;
    import java.net.NetworkInterface;
    public class Main {
        
        public static void main(String[] args){
            byte[] mac = null;
            InetAddress address = null;
            try {
                address = InetAddress.getLocalHost();
                NetworkInterface ni = NetworkInterface.getByInetAddress(address);
                mac = ni.getHardwareAddress();
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(mac.toString());
        }
    }
    but the differance I am noticing is that my pc is not returning 127.0.0.1 from getLocalHost(). for me it returns my hostname\192.168.68.69.
    try changing 'System.out.println(addr.getHostAddress());' to 'System.out.println(addr.tostring());' and see if it returns 127.0.0.1.

    if the address returned is 127.0.0.1, then the network interface returned will not be a physical interface, but the logical loopback interface.
    when I get my network interface, it is the one on my lan, id'd as eth5. that means that if the app is run with no lan network interface, it will always break there.

    Note, I am running this on windows so that may change matters.
    Things are rarely just crazy enough to work, but they're frequently just crazy enough to fail hilariously.

  10. #10
    Join Date
    Jun 2011
    Beans
    16
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: Java unique identifier

    Code:
        String getUUID(){
        	String UUID = "No_Mac_Addresss";
        	try {
        		//InetAddress addr = InetAddress.getLocalHost();
        		InetAddress addr = InetAddress.getByAddress(new byte[]{(byte) 192, (byte) 168, 1, 101});
        		//System.out.println(addr.getHostAddress());
        		System.out.println(addr.toString());
        		NetworkInterface inter = NetworkInterface.getByInetAddress(addr);
        		if(inter == null){System.out.println("null");} else {System.out.println("not null");}
        		if(inter.isUp()){System.out.println("true");} else {System.out.println("false");}
        		//byte[] test = inter.getHardwareAddress();
    		//UUID = new String(test);
        		UUID = inter.toString();
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
        	
    	System.out.println(UUID);
        	return UUID;
        }
    outputs:
    Code:
    /192.168.1.101
    true
    not null
    name:wlan0 (wlan0)
    No mac address there. I also tried to output
    Code:
    new String(inter.getHardwareAddress());
    and it outputted some question marks and a box.

    Any reason this is not returning a mac address?
    Last edited by tyzoid; July 23rd, 2011 at 10:51 PM.

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •