I am trying to use Java to create an initial registry key, and I get a really strange result. On some operating systems, such as XP, the command works flawlessly. However, in Windows 7, it only creates a key if you run a compiled jar or classes, and not from an applet on a web page. In addition, in Windows 8, the command does not work at all. I tried to debug this, and it seems that the REG command is executing successfully. If I run the command manually from the command line, it creates the keys with the same result as when running inside the program. Here is a sample 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.
In addition, the "result" of the program prints
ADD: 0
So, at this moment I am at a loss what might be the problem. I know of other risky 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 exit method?
java windows registry processbuilder
Colby
source share