9

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?

8
  • Two things jump out, 1- "and not from an applet on a web page" Applets run within a security sand box, meaning they tend to have very limited functionality when it comes to access the client machine, like running processes for example, so unless the applet is signed and has the appropriate security permissions, this will fail. 2- Windows 7+ have a different security model then XP. It could be that the Windows security manager has stepped in and stopped the action from taking place, and done so quietly. There are some good reasons for this, but I'm running out room. Commented Nov 28, 2013 at 19:41
  • You "could" try running the browser as "Admin", but I'm not sure if this will make a difference to the how the Java plugin is run... Commented Nov 28, 2013 at 19:42
  • 2
    Registry Virtualization (Windows) - MSDN - Microsoft Commented Nov 28, 2013 at 19:51
  • I should have mentioned, other processes start FINE and can use IO with no problems. The applet is signed with full clearance, write access, etc. The reg command is executing successfully, but it acts like it's being sandboxed, even when it shouldn't be. Commented Nov 29, 2013 at 6:27
  • I've even tried native code to elevate the process, still to no avail. Also I've tried to disable virtualization and it seems that I don't have permission. There must be a way to get the save access as if it ran from a cmd line at least.. Commented Dec 1, 2013 at 4:27

4 Answers 4

7
+50

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.

Sign up to request clarification or add additional context in comments.

2 Comments

How would I go about executing the 64 bit version of reg.exe? Or is there some other way around that to add to the real registry?
It must not be redirected to wow6432node because the keys need to interact with existing applications
5

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.

Comments

1

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.

1 Comment

Yes, other programs work. reg.exe is running, that's not the problem. it's the registry visualization..
1

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.

1 Comment

I've already gotten that far! The original post shows the output of reg.exe is "The operation completed successfully."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.