I'm trying the following code
class StreamGobbler extends Thread{
InputStream is;
String type;
StreamGobbler(InputStream is, String type){
this.is = is;
this.type = type;
}
public void run()
{
try
{
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null)
System.out.println(type + ">" + line);
} catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
public class GoodWindowsExec{
public static void main(String args[])
{
if (args.length < 1)
{
System.out.println("USAGE: java GoodWindowsExec <cmd>");
}
try
{
String osName = System.getProperty("os.name" );
String[] cmd = new String[3];
if( osName.equals( "Windows 8" ) )
{
cmd[0] = "cmd.exe" ;
cmd[1] = "/C";
cmd[2] = "reg query HKLM\Software\Microsoft\Cryptography /v MachineGuid";
}
else if( osName.equals( "Windows 7" ) )
{
cmd[0] = "cmd.exe" ;
cmd[1] = "/C";
cmd[2] = "reg query HKLM\Software\Microsoft\Cryptography /v MachineGuid";
}
Runtime rt = Runtime.getRuntime();
System.out.println("Execing " + cmd[0] + " " + cmd[1] + " " + cmd[2]);
Process proc = rt.exec(cmd);
// any error message?
StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
// any output?
StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");
// kick them off
errorGobbler.start();
outputGobbler.start();
// any error???
int exitVal = proc.waitFor();
System.out.println("ExitValue: " + exitVal);
} catch (Throwable t)
{
t.printStackTrace();
}
}
}
It has been tested on a computer with windows 8, it returns the value of MachineGuid, however on a computer with windows 7, when executing the .jar generated it returns me:
But when executing the code from Netbeans, it returns me: