ProcessBuilder不起作用?

时间:2014-08-21 19:24:47

标签: java windows runtime.exec processbuilder

我正在尝试使用ProcessBuilder在Windows上添加密钥注册表。在我尝试Runtime.getRuntime.exec()之前,它也不起作用。

我正在尝试这个。

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);
        }

例外情况是:

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

有什么想法吗?

2 个答案:

答案 0 :(得分:4)

reg is a cmd shell command not an executable因为它只存在于cmd.exe shell环境中。

答案 1 :(得分:0)

@Jarrod Roberson是对的。你应该使用:

Process p = Runtime.getRuntime().exec("reg add HKCU\\Software\\Microsoft\\Windows /v mykey /t REG_SZ /d " + "key_value");