All questions
/
Answer
Programming question & answer
How to Fetch System UUID with Java Program?
RRTutors community answer
Published Feb 14, 2020
Answer
We can get the System UUID by below code
public class DeviceInfo {
//Get Windows Machine UUID
public static String getWindowsDeviceUUID ()
{
try {
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 uuid=output.toString().substring(output.indexOf("\n"), output.length()).trim();;
System.out .println(uuid);
return uuid;
}catch (Exception ex)
{
System.out .println("OutPut Error "+ex.getMessage());
}
return null ;
}
//Get Mac Machine UUID
public static String getMacUUID()
{
try {
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 uuid=output.toString().substring(output.indexOf("UUID: " ), output.length()).replace("UUID: ", "" );
SerNumProcess.waitFor();
sNumReader.close();
System.out .println(uuid);
return uuid;
}catch (Exception ex )
{
}
return null ;
}
}