Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Generate a GUID/UUID from Java?

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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

609 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