2

I'm trying add a key register on windows using ProcessBuilder. Before I tried Runtime.getRuntime.exec() and doesn't work also.

I'm trying this.

ProcessBuilder p = new ProcessBuilder("reg add HKCU\\Software\\Microsoft\\Windows /v mykey /t REG_SZ /d " + "key_value");
 try {
 p.start();
 } catch (IOException ex) {
 Logger.getLogger(Registro.class.getName()).log(Level.SEVERE, null, ex);
 }

The exception is:

GRAVE: null
java.io.IOException: Cannot run program "reg add HKCU\Software\Microsoft\Windows /v mykey /t REG_SZ /d key_value": CreateProcess error=2, O sistema não pode encontrar o arquivo especificado
 at java.lang.ProcessBuilder.start(ProcessBuilder.java:1042)
 at br.com.iguana.keys.Registro.addChavesRegistro(Registro.java:50)
 at br.com.iguana.keys.Registro.main(Registro.java:158)
Caused by: java.io.IOException: CreateProcess error=2, O sistema não pode encontrar o arquivo especificado
 at java.lang.ProcessImpl.create(Native Method)
 at java.lang.ProcessImpl.<init>(ProcessImpl.java:386)
 at java.lang.ProcessImpl.start(ProcessImpl.java:137)
 at java.lang.ProcessBuilder.start(ProcessBuilder.java:1023)
 ... 2 more

Any idea ?

asked Aug 21, 2014 at 19:24
5
  • Is reg available as an executable on your path, as opposed to a shell builtin? Commented Aug 21, 2014 at 19:25
  • yep, In cmd I use reg and works. I tryied pass \\Windows\\System32 also and nothing Commented Aug 21, 2014 at 19:30
  • 1
    Yes, but does reg.exe exist as an executable? Commented Aug 21, 2014 at 19:33
  • yep...look at: i.imgur.com/Mv9G3bO.png Commented Aug 21, 2014 at 19:41
  • Separate you commands and parameters as individuals strings, this will reduce the possibility of errors... Commented Aug 21, 2014 at 20:56

2 Answers 2

4

reg is a cmd shell command not an executable as in it only exists inside the cmd.exe shell environment.

answered Aug 21, 2014 at 19:39
Sign up to request clarification or add additional context in comments.

Comments

0

@Jarrod Roberson is right. You should use:

Process p = Runtime.getRuntime().exec("reg add HKCU\\Software\\Microsoft\\Windows /v mykey /t REG_SZ /d " + "key_value");
answered Aug 21, 2014 at 20:10

6 Comments

If I use: Runtime.getRuntime.exec() without Process does work, but create reg.exe on task monitor and doesn't close this task.
I'm not sure, but call: p.destroy() should stop reg.exe
yep, I tried p.destroy(); and p.destroyForcibly(); but not stopped.
Ok, now i remember. Without using p.destroy() or something similar you can use another cmd command. Always call Runtime.getRuntime().exec(command); and pass as parameter: taskkill /im reg.exe /f. The /f switch is called to force-kill the process in case it can't be killed normally.
Just one more tip, the i'll leave you work :D Instead of create N Runtime to stop reg.exe, create a for loop
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.