Solved

Generate a GUID/UUID from Java?

Posted on 1998-08-28
5
2,691 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

718 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