我目前正在開發(fā)一個應(yīng)該執(zhí)行一些控制臺命令的程序。我的代碼如下所示: private String executeCommands(String[] commands) { String result = ""; try { ProcessBuilder pb = new ProcessBuilder(); String s = null; Charset charset = Charset.forName("IBM850"); BufferedReader stdInput; Process proc; for (String command : commands) { System.out.println("Ausfuehrung von: " + command); pb.command(command); proc = pb.start(); stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream(), charset)); while ((s = stdInput.readLine()) != null) { result += s; } System.out.println(); } } catch (Exception ex) { result = ex.getMessage(); } return result; } private void userLogIn(IUserInteraction userInteraction) { String[] command = { "svn auth --show-passwords" }; String result = executeCommands(command); System.out.println(result); }輸出是“無法運行程序“svn auth --show-passwords”:錯誤=2,沒有這樣的文件或目錄”,但是當(dāng)我在控制臺中手動輸入命令時,它起作用了。我做錯了什么?提前致謝!
2 回答
慕尼黑8549860
TA貢獻(xiàn)1818條經(jīng)驗 獲得超11個贊
經(jīng)過一些嘗試,我找到了一個可行的解決方案
String result
String line;
Process process = Runtime.getRuntime().exec(command);
Reader r = new InputStreamReader(process.getInputStream());
BufferedReader in = new BufferedReader(r);
while ((line = in.readLine()) != null)
result += line;
in.close();
喵喔喔
TA貢獻(xiàn)1735條經(jīng)驗 獲得超5個贊
svn如果您的應(yīng)用程序未攜帶環(huán)境變量,則對 jvm 沒有任何意義。
嘗試使用完整路徑執(zhí)行命令:
String[] command = { "/bin/dir1/dir2/svn auth --show-passwords" };如果您不知道程序在哪里,請使用以下命令找出它:
which svn
添加回答
舉報
0/150
提交
取消
