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 ?

5
  • Define "But not worked". Commented Oct 15, 2012 at 20:28
  • Nothing happens when I run it. Commented Oct 15, 2012 at 20:28
  • Are you running your java program with ADMINISTRATOR privileges? Commented Oct 15, 2012 at 20:29
  • you probably should look at this question: stackoverflow.com/questions/62289/… Commented Oct 15, 2012 at 20:30
  • 1
    @PabloSantaCruz HKCU doesn't need Administrator privileges. Commented Oct 15, 2012 at 20:46

4 Answers 4

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();
Sign up to request clarification or add additional context in comments.

1 Comment

It gives me this error i.imgur.com/lpEZD.jpg
1

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

1 Comment

Couldn't make it work either. Could you please be more specific ?
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.

2 Comments

Couldn't make it work either. Could you please be more specific ?
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!
0

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

2 Comments

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.
Runtime.getRuntime().exec("reg <your parameters here>"); 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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.