REG REG command does not work when running from ProcessBuilder in Java

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?

+7
java windows registry processbuilder
source share
4 answers

I assume that you have several Java virtual machines installed (32 bits, 64 bits, ...), and depending on how you execute your code, another JavaVM is used with a different result.

For example, from an applet, you usually end up in a 32-bit Java virtual machine (because the web browser is 32-bit, and therefore the virtual machine must also be 32 bits).

In this case, I assume that the 32-bit version of reg.exe is also running. In the end, everything written to HKLM\Software is redirected to HKLM\SOFTWARE\Wow6432Node (the same for HKCU\SoftwareHKCU\Software\Wow6432Node ).

In any case, I strongly recommend that you just keep track of what is really happening. Download and run Sysinternals ProcessMonitor and just look at what is written in the registry, where exactly it is written. Then you can be sure if the registry keys that you want to add are created or not, or you simply cannot find them due to any virtualization methods.

+6
source share

I created a plugin to create a registry key.

 import javaQuery.core.Registry; import javaQuery.importClass.javaQueryBundle; public class Demo { public static void main(String[] args) { String response = javaQueryBundle.createRegistry().createKey(Registry.HKEY_CURRENT_USER, "\\jqreg", Registry.key_String_Value, "Software", "javaQueryAPI"); System.out.println(response); } } 

Download the library file . If you have any problems let me know.

+5
source share

To debug this, you can try running another program, say notepad.exe, to check if it is running on the client side or not.

Then you can try using "cmd.exe / C reg" instead of "reg", it will work.

Please let me know if this works.

+1
source share

Add additional documentation:

http://technet.microsoft.com/en-us/library/cc742162.aspx

So we can use http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exec%28java.lang.String%29

Runtime.exec(String command) to execute the command and return the process.

 Process proc = Runtime.getRuntime().exec("REG ADD HKLM\Software\MyCo /v Data /t REG_BINARY /d fe340ead"); 

Now we have a Process variable that contains an expeific method: http://docs.oracle.com/javase/7/docs/api/java/lang/Process.html#getInputStream%28%29

 Process.getInputStream(); 

Let's continue our code:

 InputStream in = proc.getInputStream(); for (int i = 0; i < in.available(); i++) { System.out.println("" + in.read()); } 

I think it might be helpful.

+1
source share

All Articles