-1

I'm running a registry query using Java, and reading the result to see if Adobe is installed on the current machine. When I manually do the registry query in the command prompt, Adobe is one of the results, but my Java method can't find it.
My method to run a registry query check for Adobe:

public static void checkAdobe() 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();

        if (key.isPresent()) {
            System.out.println("Found "+key.get());
        } else {
            System.out.println("Can't find");
        }
    }
    reg.waitFor();
}

Whether I'm using local or admin command prompt, when I type:

> reg query HKEY_LOCAL_MACHINE\SOFTWARE\Policies

I get HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Adobe as one of the first results.
Why can't my program find it?

EDIT:
I have tried this (source - https://stackoverflow.com/a/30915191/9120489):

public static void main(String[] args) throws IOException, InterruptedException {
    System.out.println(getProcessOutput());
}

public static String getProcessOutput() throws IOException, InterruptedException
{
    ProcessBuilder processBuilder = new ProcessBuilder("reg", "query",
            "HKEY_LOCAL_MACHINE\\SOFTWARE\\Policies");

    processBuilder.redirectErrorStream(true);

    Process process = processBuilder.start();
    StringBuilder processOutput = new StringBuilder();

    try (BufferedReader processOutputReader = new BufferedReader(
            new InputStreamReader(process.getInputStream()));)
    {
        String readLine;

        while ((readLine = processOutputReader.readLine()) != null)
        {
            processOutput.append(readLine + System.lineSeparator());
        }
        process.waitFor();
    }
    return processOutput.toString().trim();
}

But my the result is ഊ䡋䕙彌佃䅌彍䅃䡉久屓但呗䅒䕜偯汩捩敳屁摯扥ഊ䡋䕙彌佃䅌彍䅃䡉久屓但呗䅒䕜偯汩捩敳屍楣牯獯晴instead of the actual command output.

  • Have you tried some basic debugging, for example, print out everything in the stream instead of doing filtering. You should be able to isolate whether it is problem of usage of ProcessBuilder, or problem in your filtering etc., instead of just assuming it is problem in `Optional.isPresent()` – Adrian Shum Dec 20 '17 at 01:29
  • hmmm....Your **checkAdobe()** method works fine on my system (Windows 10). So, I take it that your Console does display **Can't Find** correct? – DevilsHnd - 退職した Dec 20 '17 at 01:33
  • Try print out the entire registry output and start from there – mckuok Dec 20 '17 at 01:34
  • Yes, my result is "Can't find". How do I print out everything in the stream? – 918324876213768503759 Dec 20 '17 at 01:39
  • `output.lines().forEach(System.out::println)`, or do peeking during your stream operation: `output.lines().peek(System.out::println).filter(l -> l.contains("\\Adobe").findFirst()` – Adrian Shum Dec 20 '17 at 02:11
  • @AdrianShum Neither of those output anything. – 918324876213768503759 Dec 20 '17 at 02:29
  • What is the value returned by `reg.waitFor()`? Did the program actually run? – Jim Garrison Dec 20 '17 at 03:04
  • @JimGarrison As is, it returns "Can't find", which I have it print out if it can't find anything in the process output containing "Adobe". I know there is a result containing "Adobe" though. – 918324876213768503759 Dec 20 '17 at 04:35
  • So, I found this answer (https://stackoverflow.com/a/30915191/9120489) on a similar question, and I copied it exactly to see if I get the same output, but I just get a bunch of Chinese letters. – 918324876213768503759 Dec 20 '17 at 04:53
  • they are not meaningful chinese character. Usually caused by wrong encoding used, e.g. the command is giving UTF-16 but you are interpreting as GBK etc. To find the problem, you may get raw sequence of bytes from the process' input stream. That may give you some hints. – Adrian Shum Dec 21 '17 at 03:18

1 Answers1

0

Finally, I figured it out. I needed to specify the charset for the BufferedReader InputStreamReader:

try (BufferedReader output 
            = new BufferedReader(new InputStreamReader(reg.getInputStream(), "UTF-8"))) {