Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a computer specific ID number using Java

Tags:

java

uuid

I want to create an UUID hash using a machine specific ID number other than the MAC address to validate the computer which runs the java application on. Is it possible using Java 1.8? If so, what is the best option I can choose? It would be more helpful if it will be used for both Windows and Unix platforms.

like image 760
GeekySelene Avatar asked Mar 26 '18 09:03

GeekySelene


People also ask

What is UUID in Java?

A class that represents an immutable universally unique identifier (UUID). A UUID represents a 128-bit value. There exist different variants of these global identifiers.


2 Answers

Yes. You can do it without MAC address in both PC as well as linux systems.
I am going to break the process in steps.
Step1: Identify OS
In Your java code, identify the OS used like this

private static String OS = System.getProperty("os.name").toLowerCase();
if(OS.indexOf("win") >= 0)
//your code for windows OS.
else if(OS.indexOf("mac") >= 0)
//your code for MAC OS.
else if(OS.indexOf("sunos") >= 0)
//your code for Solaris OS
else if(OS.indexOf("nix") >= 0 || OS.indexOf("nux") >= 0 || OS.indexOf("aix") > 0 )
//your code for unix OS's

Step 2: use required commands to get the UUID of a system
What is a UUID?

A universally unique identifier (UUID) is a 128-bit number used to identify information in computer systems.

For windows

Runtime.exec("wmic csproduct get UUID");

The cmd command wmic csproduct get UUID returns the UUID of PC [windows]

For Linux
use this kernal command with Runtime.exec("YOUR COMMAND")

# cat /sys/class/dmi/id/product_uuid

To know more about Runtime.exec check this java.lang.Runtime.exec
java.lang.Runtime.exec : Through this, you supply the appropriate shell command for any underlying Environment, whether be MAC, Windows, Linux etc.

like image 181
Tahir Hussain Mir Avatar answered Oct 27 '22 02:10

Tahir Hussain Mir


Yes... You can get a computer specific ID number using Java You can use UUID of a system for specific ID of your computer.

To fetch UUID of your system by using java. Refer Following code:-

    String command = "wmic csproduct get UUID";
    StringBuffer output = new StringBuffer();

    Process SerNumProcess = Runtime.getRuntime().exec(command);
    BufferedReader sNumReader = new BufferedReader(new InputStreamReader(SerNumProcess.getInputStream()));

    String line = "";
    while ((line = sNumReader.readLine()) != null) {
        output.append(line + "\n");
    }
    String MachineID=output.toString().substring(output.indexOf("\n"), output.length()).trim();;
    System.out.println(MachineID);

}

But you can fetch only UUID of Windows system by using this code.

If you want to Fetch UUID of MAC os by using java. refer this code:

    String command = "system_profiler SPHardwareDataType | awk '/UUID/ { print $3; }'";

    StringBuffer output = new StringBuffer();


    Process SerNumProcess = Runtime.getRuntime().exec(command);

    BufferedReader sNumReader = new BufferedReader(new InputStreamReader(SerNumProcess.getInputStream()));

    String line = "";

    while ((line = sNumReader.readLine()) != null) {
        output.append(line + "\n");
    }

    String MachineID=output.toString().substring(output.indexOf("UUID: "), output.length()).replace("UUID: ", "");

    SerNumProcess.waitFor();

    sNumReader.close();

    System.out.println(MachineID);
} 

Thank You.

like image 23
Aditya Chavan Avatar answered Oct 27 '22 04:10

Aditya Chavan