Java Program for Getting System UUID for Linux Machine
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 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
- Used in creating session ID for web applications, this is also used for creating transaction ID
- It extends object class
- Used for creating random file names
- Implements Serializable and Comparable interfaces
- 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.
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.
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.