本文实例讲述了Java基于Runtime调用外部程序出现阻塞的解决方法, 是一个很实用的技巧。分享给大家供大家参考。具体分析如下:
有时候在java代码中会调用一些外部程序,比如SwfTools来转换swf、ffmpeg来转换视频等。如果你的代码这样写:Runtime.getRuntime().exec(command),会发现程序一下就执行完毕,而在命令行里要执行一会,是因为java没有等待外部程序的执行完毕,此时就需要使用阻塞,来等待外部程序执行结果:
InputStream stderr = process.getInputStream(); InputStreamReader isr = new InputStreamReader(stderr, "GBK"); BufferedReader br = new BufferedReader(isr); String line = null; while ((line = br.readLine()) != null) System.out.println(line); int exitValue = process.waitFor();
对于一般的外部程序使用上面的阻塞代码就可以,至少pdf2swf.exe是没有问题的。
但是紧接着又发现对于ffmpeg来说,以上代码会让程序卡住不动,需要使用另一种方式,封装成了一个方法,如下:
public int doWaitFor(Process process) { InputStream in = null; InputStream err = null; int exitValue = -1; // returned to caller when p is finished try { in = process.getInputStream(); err = process.getErrorStream(); boolean finished = false; // Set to true when p is finished while (!finished) { try { while (in.available() > 0) { // Print the output of our system call Character c = new Character((char) in.read()); System.out.print(c); } while (err.available() > 0) { // Print the output of our system call Character c = new Character((char) err.read()); System.out.print(c); } // Ask the process for its exitValue. If the process // is not finished, an IllegalThreadStateException // is thrown. If it is finished, we fall through and // the variable finished is set to true. exitValue = process.exitValue(); finished = true; } catch (IllegalThreadStateException e) { // Process is not finished yet; // Sleep a little to save on CPU cycles Thread.currentThread().sleep(500); } } } catch (Exception e) { e.printStackTrace(); } finally { try { if (in != null) { in.close(); } } catch (IOException e) { e.printStackTrace(); } if (err != null) { try { err.close(); } catch (IOException e) { e.printStackTrace(); } } } return exitValue; }
希望本文所述对大家Java程序设计的学习有所帮助。
以上文章从网上转载,当已经找不到出处,在实际过程中,会遇到乱码的情况
public int doWaitFor(Process process, JobInfo jobInfo) { InputStream in = null; InputStream err = null; int exitValue = -1; // returned to caller when p is finished ByteBuffer byteBuffer = ByteBuffer.allocate(512); try { in = process.getInputStream(); err = process.getErrorStream(); boolean finished = false; // Set to true when p is finished while (!finished) { try { while (in.available() > 0) { if(byteBuffer.position() == byteBuffer.capacity()) { ByteBuffer _byteBuffer = ByteBuffer.allocate(byteBuffer.capacity() + 512); _byteBuffer.put(byteBuffer.array(), 0, byteBuffer.capacity()); byteBuffer = _byteBuffer; } // Print the output of our system call byte _byte = (byte) in.read(); byteBuffer.put(_byte); Character c = new Character((char) _byte); // logMessage.append(c); System.out.print(c); } while (err.available() > 0) { if(byteBuffer.position() == byteBuffer.capacity()) { ByteBuffer _byteBuffer = ByteBuffer.allocate(byteBuffer.capacity() + 512); _byteBuffer.put(byteBuffer.array(), 0, byteBuffer.capacity()); byteBuffer = _byteBuffer; } // Print the output of our system call byte _byte = (byte) err.read(); byteBuffer.put(_byte); Character c = new Character((char) _byte); // logMessage.append(c); System.out.print(c); } // Ask the process for its exitValue. If the process // is not finished, an IllegalThreadStateException // is thrown. If it is finished, we fall through and // the variable finished is set to true. exitValue = process.exitValue(); finished = true; } catch (IllegalThreadStateException e) { // Process is not finished yet; // Sleep a little to save on CPU cycles Thread.currentThread().sleep(500); } } } catch (Exception e) { e.printStackTrace(); } finally { try { if (in != null) { in.close(); } } catch (IOException e) { e.printStackTrace(); } if (err != null) { try { err.close(); } catch (IOException e) { e.printStackTrace(); } } } try { String logMessage = new String(byteBuffer.array(), 0, byteBuffer.position(), "gbk"); System.out.print(logMessage); jobInfo.setLogMessage(logMessage); jobInfo.setExecuteResult(true); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return exitValue; }
...
相关推荐
Java SE程序 Runtime类Java SE程序 Runtime类Java SE程序 Runtime类Java SE程序 Runtime类Java SE程序 Runtime类Java SE程序 Runtime类Java SE程序 Runtime类Java SE程序 Runtime类Java SE程序 Runtime类Java SE程序...
解决Java新建进程问题 Java调用应用程序 可以学会如何使用Java的Runtime类的exec
Java SE Runtime Environment(运行时环境)包含了运行以 Java 编程语言编写的程序所必需的 Java 虚拟机、运行时类库和 Java 应用程序启动器。 Java 平台的安全性 一个由角色提供的安全信息的描述。适用于开发人员...
JRE 1.4.2 JRE(Java Runtime Environment,Java运行环境),运行JAVA程序所必须的环境的集合,包含JVM标准实现及Java核心类库。
onnx的java包,pom引入后可以试验java调用onnx文件进行cpu模型推理。 具体见https://github.com/microsoft/onnxruntime/blob/master/java/src/test/java/sample/ScoreMNIST.java 和 ...
JRE1.8.0(Java Runtime Environment)——Java运行环境32bit
Java SE Runtime Environment(运行时环境)包含了运行以 Java 编程语言编写的程序所必需的 Java 虚拟机、运行时类库和 Java 应用程序启动器。 Java 平台的安全性 一个由角色提供的安全信息的描述。适用于开发人员...
Java Runtime Environment-6.0.26.exe
JRE(Java Runtime Environment)——windows Java运行环境 64bit
Java Runtime Environment 1.6.0.7 (32-bit
Java中使用Runtime和Process类运行外部程序
Java Runtime For MacOS X 10.7
Java Runtime Environment,Java运行环境64位安装包,官网下载。 Java SE Runtime Environment 8 Downloads Do you want to run Java™ programs, or do you want to develop Java programs? If you want to run ...
通过Java运行一个外部程序 public class hpboot{ public static void main(String[] args) { String command = "C:\\Program Files\\Outlook Express\\msimn.exe"; try{ Runtime.getRuntime().exec(command);...
Sun Java SE Runtime Environment(JRE)6
基于java.lang.Runtime封装的用于简化java调用命令行的工具
JRE 版本号为8u251,macOS 64位版本,包含dmg安装程序和压缩包。 Java Runtime Environment 是运行基于Java语言编写的程序所不可缺少的运行环境,用于解释执行Java的字节码文件。
java runtime environment 1.8.0_45 64bit.rar
NULL 博文链接:https://dingherry.iteye.com/blog/1545972