//一般的执行方法,有时执行exe会卡在那 stmt要执行的命令
public static void executive(String stmt) throws IOException, InterruptedException {
Runtime runtime = Runtime.getRuntime(); //获取Runtime实例
//执行命令
try {
String[] command = {"cmd", "/c", stmt};
Process process = runtime.exec(command);
// 标准输入流(必须写在 waitFor 之前)
String inStr = consumeInputStream(process.getInputStream());
// 标准错误流(必须写在 waitFor 之前)
String errStr = consumeInputStream(process.getErrorStream()); //若有错误信息则输出
int proc = process.waitFor();
if (proc == 0) {
System.out.println("执行成功");
} else {
System.out.println("执行失败" + errStr);
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
/**
消费inputstream,并返回
*/
public static String consumeInputStream(java.io.InputStream is) throws IOException {
java.io.BufferedReader br = new java.io.BufferedReader(new java.io.InputStreamReader(is,"GBK"));
String s;
StringBuilder sb = new StringBuilder();
while ((s = br.readLine()) != null) {
System.out.println(s);
sb.append(s);
}
return sb.toString();
}