第二次,我遇到了使用ProcessBuilder从系统调用中提取值的问题。
我最后一次使用这个电话:
try {
String[] cmd = new String[5];
cmd[0] = "reg";
cmd[1] = "query";
cmd[2] = key;
cmd[3] = "/v";
cmd[4] = name;
ProcessBuilder pb = new ProcessBuilder(cmd);
Process process = pb.start();
StreamReader reader = new StreamReader(process.getInputStream());
reader.start();
int exitValue = process.waitFor();
reader.join();
if (exitValue != 0) {
return null;
}
String result = reader.getResult();
int p = result.indexOf(REGSTR_TOKEN);
if (p == -1) {
return null;
}
return result.substring(p + REGSTR_TOKEN.length()).trim();
} catch (Exception e) {
return null;
}
若要从windows注册表中提取值,请执行以下操作。
但是,该值总是返回一个错误,这与我从命令行进行调用时所发生的情况不同。可见,环境变量是不同的。
有什么问题吗?我应该设置任何环境变量吗?
发布于 2012-09-07 02:03:41
问题是Java运行时找到了错误的reg.exe
。在作为命令行执行时,当在调用我的java类\Windows\SysWOW64\reg.exe
的进程中运行时,它是作为\Windows\System32\reg.exe
执行的。每个reg.exe
指向不同的注册表。这就是虫子。
代码必须修复:
cmd[0] = "\\Windows\\System32\\reg";
或者:
cmd[0] = "\\Windows\\SysWOW64\\reg";
https://stackoverflow.com/questions/12289451
复制相似问题