我试图使用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