Solved

Generate a GUID/UUID from Java?

Posted on 1998-08-28
5
2,647 Views
Last Modified: 2012-05-04
Can anyone show me how to generate a GUID or UUID from Java code.  I am using JDK 1.2b4 and JSDK 2.0.

Thanks
0
Comment
Question by:GregL
  • 2
  • 2
5 Comments
 
LVL 8

Expert Comment

by:diakov
ID: 1222810
There are WINDOWS API functions for this purpose, for example CoCreateGUID();
Use JNI to declare a native methods in the class and then 'javah.exe' to generate the stubs. Then implement the calls with MSVC++ for example, and compile as a shared .dll.
0
 
LVL 8

Expert Comment

by:diakov
ID: 1222811
From Java, you cannot do this directly :-)
Cheers,
  Nik
0
 
LVL 1

Author Comment

by:GregL
ID: 1222812
I will reopen to see if there is an all java way.  If I don't get another answer diakov will get the points.
0
 
LVL 2

Accepted Solution

by:
threshold earned 100 total points
ID: 1222813
Do you want your JAVA GUID created as MS GUID absolutely?
Or you just want get a 'pseudo' universal unique ID for Java?

The MS GUID contains the informations in 128 bits:
1. MAC Address of LAN Card(if there is a LAN card on the host), or other Information of the host(It's a secret...)
2. time of creating the GUID

In Java, it's hard to get the first information. So we just use the IP Address or an random number to replace it.

public static String generateGUID() {
      long ip; //the 0-63th bit
      long t;  //the 64-127 bit
      try {
            byte[] ipa=java.net.InetAddress.getLocalHost().getAddress();
            ip=(long)(""+ipa[0]+ipa[1]).hashCode();
            ip=ip<<32 | ((long)(""+ipa[2]+ipa[3]).hashCode());
      } catch (java.net.UnknownHostException e) {
            ip=(long)(Math.random()*(Long.MAX_VALUE));
      }
      t=System.currentTimeMillis();
      String sip=Long.toHexString(ip);
      while (sip.length()<16)
            sip="0"+sip;
      String st=Long.toHexString(t);
      while (st.length()<16)
            st="0"+st;
      String ret=sip+st;
      ret=ret.substring(0,8)+"-"+ret.substring(8,12)+"-"+ret.substring(12,16)+"-"+ret.substring(16,20)+"-"+ret.substring(20);
      return ret;
}

Of cause, this is not a perfact Java GUID, but you can make it better:
1. if you have no LAN Card on the host, you can use the informations(user.name,user.home,...) to generate the first part of GUID
2. Mix the hashcode of IP(32 bits) with hashcode of  user.name, user.home,... (from System.getProperty ) (32 bits) to generate the first part of GUID
3. .... by your imagination


0
 
LVL 1

Author Comment

by:GregL
ID: 1222814
Thanks to all...
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now