0
我想執行的終端(在Ubuntu)的命令,似乎我無法運行該命令cd
,這裏是我的代碼:的Java - 錯誤而執行命令
public static void executeCommand(String[] cmd) {
Process process = null;
System.out.print("Executing command \'");
for (int i = 0; i < (cmd.length); i++) {
if (i == (cmd.length - 1)) {
System.out.print(cmd[i]);
} else {
System.out.print(cmd[i] + " ");
}
}
System.out.print("\'...\n");
try {
process = Runtime.getRuntime().exec(cmd);
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader err = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String line;
System.out.println("Output: ");
while ((line = in.readLine()) != null) {
System.out.println(line);
}
System.out.println("Error[s]: ");
while ((line = err.readLine()) != null) {
System.out.println(line);
}
} catch (Exception exc) {
System.err.println("An error occurred while executing command! Error:\n" + exc);
}
}
(以防萬一) 以下是我如何稱呼它: executeCommand(new String[]{ "cd", "ABC" });
有什麼建議嗎?謝謝!
你想達到什麼目的?你不能改變java的默認文件夾。它不是vb6 ...你可以通過更多的編碼獲得相同的效果。爲此需要告訴我們在此之後你想要做什麼 – tgkprog 2013-04-28 02:40:18