2

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?

Victor
  • 8,309
  • 14
  • 80
  • 129
  • 1
    Yes, your code looks fine: you should be able to read from your Java reader whatever "reg" writes to stdout. SUGGESTION: 1) run your full command from a command prompt and make sure it's legal. 2) Step through under the debugger. Examine "result"; compare it with your command-line output. – paulsm4 Sep 05 '12 at 20:47
  • An alternative is to use JNA, which has an existing API for interacting with the Windows registry. See also http://stackoverflow.com/questions/62289/read-write-to-windows-registry-using-java – Andy Thomas Sep 05 '12 at 20:53
  • Also, check out this: [Read-Write Windows Registry Using Java](http://stackoverflow.com/questions/62289/read-write-to-windows-registry-using-java) – paulsm4 Sep 05 '12 at 20:58
  • 1
    Try supplying the full path to the "reg.exe" command "c:/windows/System32/reg.exe". `ProcessBuilder` does not take into account the system path nor does it "guess" the command extension – MadProgrammer Sep 05 '12 at 20:58
  • What's `StreamReader`? I your command (with my own launcher code) and it works fine – MadProgrammer Sep 05 '12 at 21:18
  • Actually, for something under \windows\system32, I don't think a fully qualified path is necessary (but, of course, it couldn't hurt ;)) – paulsm4 Sep 05 '12 at 22:30
  • @MadProgrammer, you are right. The ProcessBuilder was calling the wrong REG.exe. This code is not mine, it's from a know application that doesn't works well in Win64! Thanks – Victor Sep 06 '12 at 13:21
  • `StreamReader` is used in a large of code snippet for this solution. I found that the code only copied a know solution for look for in the Windows Registry. – Victor Sep 06 '12 at 13:22

1 Answers1

2

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";
David M
  • 71,481
  • 13
  • 158
  • 186
Victor
  • 8,309
  • 14
  • 80
  • 129