java 在windows 执行 shell命令/cmd命令

您所在的位置:网站首页 java程序调用cmd命令 java 在windows 执行 shell命令/cmd命令

java 在windows 执行 shell命令/cmd命令

2023-08-30 00:28| 来源: 网络整理| 查看: 265

使用java调用you-get或FFmpeg等工具,实际上是使用java调用shell命令。

Runtime runtime = Runtime.getRuntime(); Process proc =runtime.exec(“cmd命令”); proc.waitFor() //容易造成主线程的阻塞。

最近使用java执行shell命令,命令返回执行结果稍长则会出现 process.waitFor() 挂起,等半天也没反应,一直处于执行状态无法结束。

一个常见原因是该过程产生一些输出而你没有从适当的流中读取。这意味着一旦缓冲区已满,该进程就会被阻止,并等待你的进程继续读取。你的进程反过来等待其他进程完成(这不会因为它等待你的进程,...)。这是一个典型的僵局。

java 1.5 以前直接使用 Runtime.getRuntime()

@Test public void t1() throws Exception { String cmdStr = "C:\\Python38\\Scripts\\you-get.exe --json https://www.iqiyi.com/v_1hz54gdreig.html"; // 执行命令, 返回一个子进程对象(命令在子进程中执行) Process process = Runtime.getRuntime().exec(cmdStr); InputStream inputStream = process.getInputStream(); InputStream errorStream = process.getErrorStream(); new Thread() { @Override public void run() { BufferedReader in = new BufferedReader(new InputStreamReader(inputStream)); String line = null; try { System.out.println("-----------------------"); while ((line = in.readLine()) != null) { System.out.println(line); } System.out.println("-----------------------"); } catch (IOException e) { e.printStackTrace(); } finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } }.start(); new Thread() { @Override public void run() { BufferedReader err = new BufferedReader(new InputStreamReader(errorStream)); try { String line2 = null; while ((line2 = err.readLine()) != null) { if (line2 != null) { System.out.println(line2); } } } catch (IOException e) { e.printStackTrace(); } finally { try { err.close(); } catch (IOException e) { e.printStackTrace(); } } } }.start(); System.out.println("等待..."); process.waitFor(15, TimeUnit.SECONDS); process.destroy(); System.out.println("over..."); }

但是我发现源码中 exec()的底层也是用的ProcessBuilder()

public Process exec(String[] cmdarray, String[] envp, File dir) throws IOException { return new ProcessBuilder(cmdarray) .environment(envp) .directory(dir) .start(); }

jdk1.5以后推荐使用 ProcessBuilder

@Test public void t1() throws Exception { ArrayList arrayList = new ArrayList(); arrayList.add("C:\\Python38\\Scripts\\you-get.exe"); arrayList.add("--json"); arrayList.add("https://www.iqiyi.com/v_1hz54gdreig.html"); // 执行命令, 返回一个子进程对象(命令在子进程中执行) ProcessBuilder processBuilder = new ProcessBuilder(); Process process = processBuilder.command(arrayList).start(); InputStream inputStream = process.getInputStream(); byte[] bytes = new byte[1024]; String line; System.out.println("-----------获得数据-------------"); while (inputStream.read(bytes)!=-1){ line=new String(bytes,"utf-8"); System.out.println("获得数据:" + line); } System.out.println("-------------------------------"); inputStream.close(); InputStream errorStream = process.getErrorStream(); bytes = new byte[1024]; while (errorStream.read(bytes)!=-1){ line=new String(bytes,"utf-8"); System.out.println("获得errorStream数据:" + line); } errorStream.close(); } 建议大家根据自己命令的耗时来选择这两种方式


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3