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
FernandoPaiva
4,45214 gold badges63 silver badges127 bronze badges
2 Answers 2
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
user177800
Sign up to request clarification or add additional context in comments.
Comments
@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
Marcello Davi
4435 silver badges17 bronze badges
6 Comments
FernandoPaiva
If I use:
Runtime.getRuntime.exec() without Process does work, but create reg.exe on task monitor and doesn't close this task.Marcello Davi
I'm not sure, but call:
p.destroy() should stop reg.exeFernandoPaiva
yep, I tried
p.destroy(); and p.destroyForcibly(); but not stopped.Marcello Davi
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.Marcello Davi
Just one more tip, the i'll leave you work :D Instead of create N Runtime to stop reg.exe, create a for loop
|
Explore related questions
See similar questions with these tags.
lang-java
regavailable as an executable on your path, as opposed to a shell builtin?regand works. I tryied pass\\Windows\\System32also and nothingreg.exeexist as an executable?