碰到一个项目需要从Java中运行Perl程序,这个Perl程序调用客户的Web service,每次发送一个请求,接受一个响应。Java程序中包含多个请求,需要多次调用Perl程序,并且接受和解析响应(这个烂设计可不是我干的,我实在不明白强大的Java Web Service为什么要弄成这样,不过客户是老大)。使用Java Runtime的exec()方法,发现运行一段时间后,进程就被挂起了(之前的响应完全正确)。于是分析原因,发现我在运行exec()方法后,立刻执行了Process的waitFor()方法,这里出了问题。在网上找到一篇文章讲述这个问题:
地址:
http://brian.pontarelli.com/2005/11/11/java-runtime-exec-can-hang/
November 11, 2005 on 4:40 pm | In Java |
The next version of Savant is going to focus heavily on the stand-alone runtime and support for dialects and plugins. Supporting all that is largely handled by using a simple executor framework I wrote around Java 1.4 and lower’s Runtime.exec method. A few things to keep in mind when using this:
- Always read from the streams prior to calling waitFor. Otherwise you could end up waiting forever on Windows and other OS platforms whose I/O buffers can’t store enough from standard out and standard error to ensure the program has finished. These platforms will pause the execution of whatever is running until something reads the buffered content from standard out and standard error. I would imagine all platforms suffer from this, but some platforms have larger buffers than others. Needless to say, always read from the streams first.
- Always read from standard error first. I ran across a bug where some OS platforms will always open standard out, but never close it. What this means is that if you read from standard out first and the process only writes to standard error, you’ll hang forever waiting to read. If you read from standard error first, you’ll always be okay on these platforms because the OS seems to shutdown standard error. I think however, that the best way to handle all cases is to check both standard error and standard out for readiness and only read from them if they have something to offer. The downside I could see here is that error isn’t ready, but eventually will be.
可以看出:
- 永远要在调用waitFor()方法之前读取数据流
- 永远要先从标准错误流中读取,然后再读取标准输出流
于是将waitFor()方法放在读取数据流后调用,目前没有发现什么问题。
分享到:
相关推荐
JRE 1.4.2 JRE(Java Runtime Environment,Java运行环境),运行JAVA程序所必须的环境的集合,包含JVM标准实现及Java核心类库。
JRE(Java Runtime Environment)——windows Java运行环境 64bit
JRE1.8.0(Java Runtime Environment)——Java运行环境32bit
Java Runtime Environment-6.0.26.exe
Java Runtime Environment 1.6.0.7 (32-bit
Java Runtime For MacOS X 10.7
java runtime environment 1.8.0_45 64bit.rar
java runtime environment 1.8.0_45 64bit jre1.8.0_45 64bit jre-8u261
jre(Java Runtime Environment),Java运行环境64位安装包
Java SE Runtime Environment(运行时环境)包含了运行以 Java 编程语言编写的程序所必需的 Java 虚拟机、运行时类库和 Java 应用程序启动器。 Java 平台的安全性 一个由角色提供的安全信息的描述。适用于开发人员...
毕业时的外文翻译原文 A Race-Aware Java Runtime
Java SE Runtime Environment(运行时环境)包含了运行以 Java 编程语言编写的程序所必需的 Java 虚拟机、运行时类库和 Java 应用程序启动器。 Java 平台的安全性 一个由角色提供的安全信息的描述。适用于开发人员...
jre(Java Runtime Environment),Java运行环境32位安装包
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 runtime environment(JRE)安装 2020-11-4 一、搭建环境 1.1 安装Keil 5 官网下载:http://www2.keil.com/mdk5/ 百度网盘:https://pan.baidu.com/s/18t_ta0WWX_f1KCKXfgj_Zw 提取码:gx3r 1.2 安装JRE 由于...
ArcGIS Runtime SDK for Java 100.12.0 的...如需在生产环境中使用ArcGIS Runtime SDK for Java,请联系ESRI公司获得授权,现在也有国产替代产品GeoScne Runtime SDK for Java,可以联系易智瑞公司获得相关资源和授权。
Java Runtime Environment-6.0.450
Installation notes for the J2SE Runtime Environment 5.0 (JRE), including system requirements and installation instructions, are available for the following platforms. Platform 32-bit Installation 64...
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 环境