我在我的项目中使用WinRegistry类,这是我的代码(Courtesy of Cravenica):
public static void checkInstalled(){
try {
String regValue = null;
regValue = WinRegistry.valueForKey(
WinRegistry.HKEY_LOCAL_MACHINE,
"SOFTWARE\\Policies",
"Adobe");
if(regValue == null) {
System.out.println("Application not installed");
} else {
System.out.println("Application is installed");
}
} catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException | IOException ex) {
System.err.println(ex);
}
}
代码应该相当简单。我正在尝试通过查看注册表目录来检查用户计算机上是否安装了Adobe应用程序:“HKEY_LOCAL_MACHINE \ SOFTWARE \ Policies”
用于密钥:“Adobe”。
但是,我收到此错误:
“java.lang.IllegalArgumentException:系统找不到指定的路径:'HKEY_LOCAL_MACHINE \ SOFTWARE \ Policies'”。< BR />
我也试过这个:
List<String> ls = WinRegistry.subKeysForPath(
WinRegistry.HKEY_LOCAL_MACHINE,
"SOFTWARE\\Policies");
String adobeKey = ls.stream().filter(st -> st.matches("Adobe")).findAny().get();
但 ls 返回带有NullPointerException的“null”,我得到同样的错误:
“java.lang.IllegalArgumentException:系统找不到指定的路径:'HKEY_LOCAL_MACHINE \ SOFTWARE \ Policies'”
我试过这个(Courtesy of VGR):
public static void checkInstalled()
throws IOException, InterruptedException{
ProcessBuilder builder = new ProcessBuilder(
"reg", "query", "HKEY_LOCAL_MACHINE\\SOFTWARE\\Policies");
Process reg = builder.start();
try (BufferedReader output = new BufferedReader(
new InputStreamReader(reg.getInputStream()))) {
Stream<String> keys = output.lines().filter(l -> !l.isEmpty());
Stream<String> matches = keys.filter(l -> l.contains("\\Adobe"));
Optional<String> key = matches.findFirst();
}
reg.waitFor();
}
但键返回“空”。
我知道它在那里。发生什么了?
我现在打开注册表,我正在HKLM \ SOFTWARE \ Policies中查看Adobe密钥。为什么我的程序看不到它?
更新:我在另一台计算机上重新创建了整个项目,但我没有错误。所以,我回去并在原始计算机上重新创建了整个项目,并再次收到错误。