I need to access the MachineGuid value of the computer's windows registry, I use this:
registryPath = "HKLM\Software\Microsoft\Cryptography";
registryKey = "MachineGuid";
Process process = Runtime.getRuntime().exec("reg query " + '"' + registryPath + "\" /v " + registryKey);
To subsequently obtain only the value of MachineGuid.
StreamReader reader = new StreamReader(process.getInputStream());
reader.start();
process.waitFor();
reader.join();
String output = reader.getResult();
// Parse out the value
String[] parsed = output.split(" ");
StringTokenizer stringMachineGuid = new StringTokenizer(parsed[parsed.length - 1]);
String stringSinBlancos = null;
while (stringMachineGuid.hasMoreElements()) {
stringSinBlancos += stringMachineGuid.nextElement();
}
String subString = stringSinBlancos.substring(4,stringSinBlancos.length());
return subString;
It's from an example that I found, in Windows 8 it works fine, but in Windows 7 it returns an empty pointer exception.
Searching the Internet I have tried the following ways:
Process process = Runtime.getRuntime().exec("reg query HKLM\Software\Microsoft\Cryptography /v MachineGuid");
Process process = Runtime.getRuntime().exec(new String []{"reg", "query", "HKLM\Software\Microsoft\Cryptography", "/v", "MachineGuid"});
String s = "reg query HKLM\Software\Microsoft\Cryptography /v MachineGuid"; Runtime.getRuntime.exec(new String [] {"bash", "-c", s});
Unsuccessful.