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?

Chris
  • 1,416
  • 18
  • 29
Colby
  • 452
  • 4
  • 19
  • 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. – MadProgrammer Nov 28 '13 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... – MadProgrammer Nov 28 '13 at 19:42
  • 2
    [Registry Virtualization (Windows) - MSDN - Microsoft](http://msdn.microsoft.com/en-us/library/windows/desktop/aa965884(v=vs.85).aspx) – Holger Nov 28 '13 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. – Colby Nov 29 '13 at 06: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.. – Colby Dec 01 '13 at 04:27
  • Have you tried to use `regedit` instead? – Gabe Dec 01 '13 at 04:56
  • How would I go about using regedit from Java? Keep in mind that if I run the jar from cmd line it IS able to make the keys. The problem lies somewhere in running the jar by double click or from the web. – Colby Dec 01 '13 at 12:39
  • Please post a concrete example what keys you want to create because that may also affect the result. – Robert Dec 04 '13 at 12:25

4 Answers4

7

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.

Robert
  • 39,162
  • 17
  • 99
  • 152
  • 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? – Colby Dec 05 '13 at 03:58
  • It must not be redirected to wow6432node because the keys need to interact with existing applications – Colby Dec 05 '13 at 04:10
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.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
Vicky Thakor
  • 3,847
  • 7
  • 42
  • 67
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.

Ankit Zalani
  • 3,068
  • 5
  • 27
  • 47
  • Yes, other programs work. reg.exe is running, that's not the problem. it's the registry visualization.. – Colby Dec 08 '13 at 17:49
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.

Marcos Eusebi
  • 617
  • 7
  • 17
  • I've already gotten that far! The original post shows the output of reg.exe is "The operation completed successfully." – Colby Dec 08 '13 at 19:42