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.
-
Why not the MAC address? – Federico klez Culloca Mar 26 '18 at 09:58
-
In case the computer doesn't have a network card installed. – GeekySelene Mar 26 '18 at 09:59
-
Is that common in what you think your target machines will be or are you just being very very cautious? – Federico klez Culloca Mar 26 '18 at 10:00
-
4Possible duplicate of [How to get a unique computer identifier in Java (like disk id or motherboard id)](https://stackoverflow.com/questions/1986732/how-to-get-a-unique-computer-identifier-in-java-like-disk-id-or-motherboard-id) – Georg Muehlenberg Mar 26 '18 at 10:01
-
Hey, please look at the answers to [this question](https://stackoverflow.com/questions/1986732/how-to-get-a-unique-computer-identifier-in-java-like-disk-id-or-motherboard-id). You can get all kinds of serial numbers, e.g. of the motherboard. – Georg Muehlenberg Mar 26 '18 at 10:02
-
Most of the solutions are stated for Windows environment. Is there any solution I can use for both Unix and Windows environments? – GeekySelene Mar 26 '18 at 10:23
-
@smartJohnDoe check my answer. And do let me know if it worked. – Tahir Hussain Mir Mar 26 '18 at 11:02
-
Guess you want some sort of copy-protection or licensing or something like it. You're wasting your time. – Lorinczy Zsigmond Mar 26 '18 at 14:19
3 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.

- 2,506
- 2
- 19
- 26
-
thanks. you saved the day. Can you specify how to get the command output into a string when the command needs permission to run. – GeekySelene Mar 26 '18 at 12:47
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.

- 61
- 1
- 3
-
Please remove the question from your answer. This is not a usual forum but a Q&A site with a clear distinction between questions and answers. To get an answer to your question ask it as a new question :) – geisterfurz007 Jun 28 '18 at 07:09
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();
}

- 5,179
- 10
- 35
- 56

- 847
- 3
- 13
- 31