0
我想從java運行cmd.exe命令(例如md C:\ blabla創建一個新目錄C:\ blabla) 我的代碼看起來像這樣,它運行沒有任何錯誤:使用apache commons運行簡單的cmd.exe命令exec
import java.io.IOException;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
public class Test {
public static void main(String[] args) throws ExecuteException, IOException {
CommandLine cmdLine = new CommandLine("cmd.exe");
cmdLine.addArgument("md");
cmdLine.addArgument("C:\\blabla");
DefaultExecutor executor = new DefaultExecutor();
executor.execute(cmdLine);
}
}
但是,如果我去到C:\沒有文件夾作爲布拉布拉我所期望的,因爲手動鍵入MD C:\布拉布拉輸入cmd.exe正常工作。我也試過「C:\ Windows \ System32 \ cmd.exe」而不是「cmd.exe」,但沒用。
在控制檯輸出看起來是這樣的:
的Microsoft Windows [版本6.1.7601] 版權所有(C)2009年微軟公司。版權所有。
C:\用戶\ Selphiron \工作區\測試>
哪裏錯了嗎?
Thx爲您的答案。它工作,但令我驚訝的是,當我打開cmd.exe並鍵入md C:\ blabla時,新目錄被創建(Windows 7 64位)。 反正謝謝:) – Selphiron 2014-11-05 16:25:01
只要給''cmd.exe'作爲參數的命令並不意味着cmd接受它並執行它。檢查''cmd.exe /?''來檢查參數的作用。 – f1sh 2014-11-05 19:22:20