/**
* 执行Cmd命令方法
*
* @param command 相关命令
* @return 执行结果
*/
// List<String> commands 是cmd命令每个字符的集合
public void runCommand(List<String> command) {
ProcessBuilder builder = new ProcessBuilder(command);
builder.redirectErrorStream(true);
try {
Process process = builder.start();
final StringBuilder stringBuilder = new StringBuilder();
final InputStream inputStream = process.getInputStream();
new Thread(new Runnable() {//启动新线程为异步读取缓冲器,防止线程阻塞
@Override
public void run() {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
try {
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
process.waitFor();
} catch (Exception e) {
throw new RuntimeException("ffmpeg执行异常" + e.getMessage());
}
}
执行cmd命令
最新推荐文章于 2024-08-03 10:53:31 发布