Solved

Generate a GUID/UUID from Java?

Posted on 1998-08-28
5
2,676 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

820 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