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.