GeekySelene
GeekySelene

Reputation: 927

How to get a computer specific ID number using Java

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.

Upvotes: 11

Views: 14217

Answers (3)

GeekySelene
GeekySelene

Reputation: 927

Just like this the UUID can be retrieved as a string according to the platform.

String OS = System.getProperty("os.name").toLowerCase();
String machineId = null;
if (OS.indexOf("win") >= 0) {
    StringBuffer output = new StringBuffer();
    Process process;
    String[] cmd = {"wmic", "csproduct", "get", "UUID"};  
    try {
        process = Runtime.getRuntime().exec(cmd);
        process.waitFor();
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line = "";
        while ((line = reader.readLine()) != null) {
            output.append(line + "\n");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    machineId = output.toString();
} else if (OS.indexOf("nix") >= 0 || OS.indexOf("nux") >= 0 || OS.indexOf("aix") > 0) {

    StringBuffer output = new StringBuffer();
    Process process;
    String[] cmd = {"/bin/sh", "-c", "echo <password for superuser> | sudo -S cat /sys/class/dmi/id/product_uuid"};
    try {
        process = Runtime.getRuntime().exec(cmd);
        process.waitFor();
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line = "";
        while ((line = reader.readLine()) != null) {
            output.append(line + "\n");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    machineId = output.toString();
}

Upvotes: 4

Aditya Chavan
Aditya Chavan

Reputation: 61

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.

Upvotes: 6

Tahir Hussain Mir
Tahir Hussain Mir

Reputation: 2626

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.

Upvotes: 7

Related Questions