Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows REG command not working when executed 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?

like image 771
Colby Avatar asked Nov 28 '13 18:11

Colby


4 Answers

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.

like image 115
Robert Avatar answered Nov 09 '22 18:11

Robert


I developed plugin to create key in registry.

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 library file ,If you have any problem do let me know.

like image 29
Vicky Thakor Avatar answered Nov 09 '22 18:11

Vicky Thakor


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

You can then try with "cmd.exe /C reg" instead of "reg", it will work.

Kindly let me know if it works.

like image 1
Ankit Zalani Avatar answered Nov 09 '22 20:11

Ankit Zalani


Reg add 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 a Process.

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

Now we have the Process variable wich contain a expesific method: http://docs.oracle.com/javase/7/docs/api/java/lang/Process.html#getInputStream%28%29

Process.getInputStream();

Let's continue with our code:

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

I guess this can be kinda helpful.

like image 1
Marcos Eusebi Avatar answered Nov 09 '22 19:11

Marcos Eusebi