0

Possible Duplicate:
read/write to Windows Registry using Java

I'm trying to run this cmd code in java.

REG ADD "HKCU\SOFTWARE\MICROSOFT\INTERNET EXPLORER\MAIN" /V "START PAGE" /D "http://www.google.com/" /F

This works perfectly with bat file. I have tried to make it work on java

import java.util.*;

private static void addToWin( File f, String param ) throws IOException {
String name = generateName(f);
String cmd = "REG ADD HKCU\\Software\\Microsoft\\Internet Explorer\\Main /V Start Page /D http://www.google.com/ /F";
Runtime.getRuntime().exec(cmd);
}

But not worked. How can I make this work ?

Community
  • 1
  • 1
user198989
  • 4,574
  • 19
  • 66
  • 95

4 Answers4

4

Here you are:

ProcessBuilder pb = new ProcessBuilder(new String[]{"REG", "ADD", "HKCU\\Software\\Microsoft\\Internet Explorer\\Main", "/v", "Start Page", "/d", "\"http://www.google.com/\"", "/f"});
pb.start();
Manitoba
  • 8,522
  • 11
  • 60
  • 122
1

You need to quote command line arguments that contain spaces: \\Internet Explorer\\

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
1

You are missing the \" quotes.

So the command is misinterpreted because it contains spaces. Quoting is essential!

Try adding a simple

 System.err.println(cmd);

(or use your favorite logger). Pay attention to the missing quotation marks. If the printed string is not identical to the command you want to execute, it is not surprising it doesn't work.

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
  • Couldn't make it work either. Could you please be more specific ? – user198989 Oct 15 '12 at 21:59
  • 1
    See updated answer, with a hint on debugging. Your `String cmd` is not correct. It must have the same `"` characters in it! And it is helpful if you edit your question with things that you tried - we cannot see what is on your screen! – Has QUIT--Anony-Mousse Oct 15 '12 at 22:15
0

If you are willing to READ/WRITE from/into Windows registry I would recommend you to take a look at this question.

Community
  • 1
  • 1
Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
  • Since the question includes answers detailing how to read and write to the registry using shell calls and the REG utility, I'm going to vote to close this question as an exact duplicate. – Wug Oct 15 '12 at 20:34
  • Runtime.getRuntime().exec("reg "); This mentioned in that link. But not works, because reg line is not executable, so that you need to run cmd to do that. That's why I asked my question. – user198989 Oct 15 '12 at 20:41