?
Solved

Generate a GUID/UUID from Java?

Posted on 1998-08-28
5
Medium Priority
?
2,740 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 400 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month16 days, 23 hours left to enroll

864 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