Victor
Victor

Reputation: 8480

Understanding ProcessBuilder

For the second time I had a problem with values extract from system calls using ProcessBuilder.

The last time I used the call:

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;
}

To extract a value from windows registry.

But the value always return an error, different from what happens if I make the call from the command line. It's seens that the enviroment variables are differents.

What's the problem? I should set any enviroment variable?

Upvotes: 2

Views: 834

Answers (1)

Victor
Victor

Reputation: 8480

The problem was that the Java runtime found the wrong reg.exe. When executing as command line, it was executed as \Windows\System32\reg.exe, when running inside the process that call my java class calls \Windows\SysWOW64\reg.exe. Each reg.exe points to differents Registry tables. That was the bug.

The code must be fixed:

cmd[0] = "\\Windows\\System32\\reg";

Or:

cmd[0] = "\\Windows\\SysWOW64\\reg";

Upvotes: 2

Related Questions