Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Java Program for Getting System UUID for Linux Machine

  • Difficulty Level : Medium
  • Last Updated : 02 Aug, 2021

Universally Unique Identifiers (UUIDs) are also known as Globally Unique Identifier(GUID) which are 128-bit numbers and are unique on all local systems they are created on also UUIDs created among other systems. Java UUID class is a part of java.util package. Java UUID class represents an immutable universally unique identifier. UUID is a combination of 8-4-4-4-12 characters.

UUID is made up of hex digits (4 char each) along with 4 “-” symbols which makes its length equal to 36 characters. A UUID can have all bits set to zero which is also known as Nil UUID

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

Structure of UUID

Structure of UUID Terms

  • time_low: low 32-bits of time
  • time_mid: middle 16-bits of time
  • time_hi_and_version: 4-bit version in the MSB, followed by high 12-bits of the time
  • clock_seq_hi_and_res_clock_seq_low: 1-3 bit variant in the most significant bits followed by 13-15 bit clock sequence
  • node: 48-bit node ID

Applications of Java UUID



  1. Used in creating session ID for web applications, this is also used for creating transaction ID
  2. It extends object class
  3. Used for creating random file names
  4. Implements Serializable and Comparable interfaces
  5. Used to generate primary key for Database Table

Methods to generate Java UUID

Version 1: Generating node() value for time based UUID

Here, the method returns the long value of the node from UUID, and to fetch node value we need to create a time-based (version 1) UUID. It returns the node value associated with the UUID. It is a 48-bit node value.  It holds the IEEE 802 address (MAC Address) of the machine. 

Example:

Java




// Java program for Generating node()
// value for time based UUID
 
//import UUID package
import java.util.UUID;
public class UUIDMain {
    public static void main(String arg[])
    {
        // returns a UUID with specified value
        UUID uuid = UUID.fromString("237e9877-e79b-12d4-a765-321741963000");
       
        // returns node value
        System.out.println("Node value: "
            + uuid.node());
       
      System.out.println("UUID Version : "
                           + uuid.version());
    }
}

 
 

Output
Node value: 55075465998336
UUID Version : 1

 

Version 2: Name based UUID



 A Static factory method used to fetch name-based UUID using the specified byte array. It uses “name” in the broadcast sense imaginable and a UUID indicating the type of name used. 

Example:

Java




// Java program for generating Name based UUID
 
import java.util.UUID;
public class Main {
 
    public static void main(String[] args)
    {
 
        // initialize byte array
        byte[] bs = { 1, 2, 3 };
       
        // initialize uuid using byte array
        UUID uuid = UUID.nameUUIDFromBytes(bs);
       
        System.out.println("UUID : " + uuid);
        System.out.println("UUID Version : "
                           + uuid.version());
    }
}

 
 

Output
UUID : 5289df73-7df5-3326-bcdd-22597afb1fac
UUID Version : 3

 

UUID for Linux Machine 

The above two approaches can be used to generate UUID for the Linux system. To generate a system UUID for Linux Machine there are some pre-requisites needed before generating a UUID.
 

  • Java Development Kit(JDK) should be installed, the latest version would work much better here
  • Setting up of Vim or Gedit editor for compiling and running the code is needed.
  • Basics of working with Vim editor required to compile, save, run and edit the program.
  • Knowledge of running code in both Vim Editor and Linux Kernel/Terminal with the help of Linux commands.

For the code given below, Vim Editor is used to compiling and running the program. UUID is a unique identifier used in partitions to uniquely identify partitions in Linux operating systems. UUID is a property of the disk partition itself. 

In the code below, we import the Buffer reader and Input Stream Reader package, then we generate a random UUID with the help of the String method to get the inputs from it and generating UUID.  

Java




// Java Program to get System UUID Number for linux Machine
// (Root User Login is Req for running the code)
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
 
public class Main {
 
    // Method for get System UUID for Linux Machine
    static String getLinuxSystem_UUID()
    {
        String command = "dmidecode -s system-uuid";
 
        // setting uuid to null first
        String uuid = null;
        try {
            Process SerNumProcess
                = Runtime.getRuntime().exec(command);
            BufferedReader sNumReader
                = new BufferedReader(new InputStreamReader(
                    SerNumProcess.getInputStream()));
 
            // reads the uuid line by line to separate the
            // uuid into 4 parts
            uuid = sNumReader.readLine().trim();
 
            SerNumProcess.waitFor();
            sNumReader.close();
        }
        catch (Exception ex) {
            System.err.println("Linux UUID Exp : "
                               + ex.getMessage());
            uuid = null;
        }
        finally {
            return uuid;
        }
    }
 
    public static void main(String[] args)
    {
        // generates uuid for linux system
        String system_uuid = getLinuxSystem_UUID();
 
        System.out.println("Linux System UUID Number : "
                           + system_uuid);
    }
}

 
 

Output:

System UUID for Linux Machine

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!