WinRegistry IllegalArgumentException error can't find path: 'HKEY_LOCAL_MACHINE\SOFTWARE\Policies'

Viewed 294

I'm using the WinRegistry class in my project and this is my code (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);
    }
}

The code should be fairly straightforward. I'm trying to check if an Adobe application is installed on the user's machine by looking in the registry directory: "HKEY_LOCAL_MACHINE\SOFTWARE\Policies"
for the key: "Adobe".
However, I'm getting this error:
"java.lang.IllegalArgumentException: The system can not find the specified path: 'HKEY_LOCAL_MACHINE\SOFTWARE\Policies'".


I've also tried this:

List<String> ls = WinRegistry.subKeysForPath(
              WinRegistry.HKEY_LOCAL_MACHINE,
              "SOFTWARE\\Policies");
String adobeKey = ls.stream().filter(st -> st.matches("Adobe")).findAny().get();

But ls returns "null" with a NullPointerException, and I get the same error:
"java.lang.IllegalArgumentException: The system can not find the specified path: 'HKEY_LOCAL_MACHINE\SOFTWARE\Policies'"


And I tried this (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();
}

But key returns "empty".

I know it's there. So what's going on? I have the registry open right now and I'm looking at the Adobe key right there in HKLM\SOFTWARE\Policies. Why does my program not see it??

Update: I recreated the whole project on a different computer and I did not have the error. So, I went back and recreated the whole project on the original computer, and got the error again.

0 Answers
Related