java 调用系统指令
String msg = ""; Process process = null; try { ProcessBuilder pb = new ProcessBuilder(); //因为process执行命令并不是像窗口中执行shell一样,所以需要添加参数,用于执行脚本 // pb.command(ffmpegPath, "-y", "-i", filePath, "-ss", n + "", "-f", "image2", targetUrl);
pb.command("这里写指令");
//processBuilder支持将inputStream与ErrorStream合并为一个Stream,即所有的输出信息都合并到inputStream中,这样做可以减少一个线程 pb.redirectErrorStream(true); process = pb.start(); //由于process机制原因会导致死锁,所以需要在waitfor方法之前,创建线程用于处理inputstream中缓冲区的数据,这也是为什么要合并inputstream和errorstream的原因,在这里可以少创建一个线程 readInputStream(process.getInputStream()); //返回0则表示输出正常 int resultCode = process.waitFor(); } catch (Exception e) { msg = ExcBox.getExcMsg(e); log.info(msg); } finally { try { if (null != process) { process.getErrorStream().close(); process.getInputStream().close(); process.getOutputStream().close(); } } catch (Exception ignored) { } }
//创建线程处理输出流【其实就是真实的写指令】 private static void readInputStream(InputStream in) { executor.execute(new Runnable() { @Override public void run() { InputStreamReader reader = null; try { reader = new InputStreamReader(in); LineNumberReader line = new LineNumberReader(reader); String str = null; while ((str = line.readLine()) != null) { System.out.println(str); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (reader != null) { reader.close(); } } catch (IOException e) { e.printStackTrace(); } } } }); }
本文来自博客园,作者:岑惜,转载请注明原文链接:https://www.cnblogs.com/c2g5201314/p/16943672.html
响应开源精神相互学习,内容良币驱除劣币
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)