The Windows REG command does not work when run from ProcessBuilder in Java


I'm trying to use Java to create a start up registry key and I'm getting a really weird result. On some OS's such as XP the command works flawlessly. However, on Windows 7 it only creates the key if you run the compiled jar or classes, and not from an applet on a web page. Additionally on Windows 8, the command does not work at all. I've tried debugging this, and it seems that the REG command is executing successfully. If I run the command manually from command prompt, it creates the keys, with the same output as when it's ran from inside the program. Heres an example of the code:

public static int regadd(String key, String name, String val) throws IOException, InterruptedException {
    ProcessBuilder pb = new ProcessBuilder(new String[]{"REG", "ADD", key, "/v", name, "/d", val, "/f"});
    pb.redirectOutput(new File(PathManager.getDirectory(), "log0.txt"));

    int i = pb.start().waitFor();
    Logger.log("ADD: " + i);

    return i;

In log0.txt it prints this:

The operation completed successfully.

Additionally, the "result" of the program prints

ADD: 0

So at this point, I am at a loss of what could be the problem. I am aware of the other dicey ways to add to the registry, but I would like to keep my code compatible with all VM distributions. Is there a way to accomplish this or fix the exiting method?

I assume that you have multiple Java VMs installed (32 bit, 64bit,...) and depending how you execute your code a different JavaVM is used with a different result.

For example from within an applet you usually end up in the 32 bit Java VM (because the web-browser is 32bit and therefore the VM also has to be 32bit).

In such a case I assume that also the 32bit versuon of reg.exe is executed. In the end everything written to HKLM\Software is redirected to HKLM\SOFTWARE\Wow6432Node (same for HKCU\Software -> HKCU\Software\Wow6432Node).

In any case I strongly recommend to you just to monitor what is really going on. Download and start Sysinternals ProcessMonitor and simply look up what is written to the registry where exactly it is written to. Then you can be sure if or if not the registry keys you want to add are created or not or if you simply don't find them because of any of the virtualization techniques.