我試圖使用getRuntime().exec()運行cmd命令。問題是,它只能如果我的命令字符串是IPCONFIG,而如果我嘗試像Java運行命令-version或時間,它然後引發錯誤:java getRuntime().exec()對於運行基本cmd命令不起作用
產生java.io.IOException:不能運行程序 「時間」:CreateProcess的錯誤= 2,系統找不到指定的文件
String command = "time";
Process p;
try {
p = Runtime.getRuntime().exec(command);
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while (true) {
line = r.readLine();
if (line == null) { break; }
//System.out.println(line);
textField_1.setText(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
*系統找不到指定的文件*,所以也許不是你的道路? –
「time」命令是否在你的'cmd'中工作? –
因爲包含java程序的文件夾不在您的環境路徑中。將其添加到路徑中或指定java命令的絕對路徑。您可以在運行時評估一個屬性,而不是在代碼中對其進行硬編碼。 – davidxxx