java 中判断exe文件执行结束方法
2012-02-15 20:32
477 查看
参考地址:
http://hi.csdn.net/qybao
javadoc
The methods that create processes may not work well for special processes on certain native platforms, such as native windowing processes, daemon processes, Win16/DOS processes on Microsoft Windows, or
shell scripts. The created subprocess does not have its own terminal or console. All its standard io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process through three
streams (getOutputStream(), getInputStream(), getErrorStream()). The parent process uses these streams to feed input to and get output from the subprocess. Because some native platforms only provide limited
buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.
最后一段话,死锁的原因是子线程的输出流或输入流缓存太小,所以必须自己手动清空缓存。
在process.waitFor();之前加上一下代码
BufferedReader br = new BufferedReader(process.getInputStream());
while(br.readLine()!=null);
通常这样可以解决,但是线程执行是由cpu控制的,如果process还没被执行,那么while(br.readLine()!=null);就会结束,此时如果process刚好被执行了,但由于while(br.readLine()!=null);已经结束了,process的输入流输出流还是没有被清空,到process.waitFor();时还是会造成堵塞的。
所以,一般我的做法是把上面的代码写到一个监视线程中,比如
class WatchThread extends Thread {
Process p;
boolean over;
public WatchThread(Process p) {
this.p = p;
over = false;
}
public void run() {
try {
if (p == null) return;
BufferedReader br = new BufferedReader(p.getInputStream());
while (true) {
if (p==null || over) {
break;
}
while(br.readLine()!=null);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void setOver(boolean over) {
this.over = over;
}
}
然后在process.waitFor()之前,添加
WatchThread wt = new WatchThread(process);
wt.start();
然后在process.waitFor()之后,添加
wt.setOver(true);
http://hi.csdn.net/qybao
javadoc
The methods that create processes may not work well for special processes on certain native platforms, such as native windowing processes, daemon processes, Win16/DOS processes on Microsoft Windows, or
shell scripts. The created subprocess does not have its own terminal or console. All its standard io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process through three
streams (getOutputStream(), getInputStream(), getErrorStream()). The parent process uses these streams to feed input to and get output from the subprocess. Because some native platforms only provide limited
buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.
最后一段话,死锁的原因是子线程的输出流或输入流缓存太小,所以必须自己手动清空缓存。
在process.waitFor();之前加上一下代码
BufferedReader br = new BufferedReader(process.getInputStream());
while(br.readLine()!=null);
通常这样可以解决,但是线程执行是由cpu控制的,如果process还没被执行,那么while(br.readLine()!=null);就会结束,此时如果process刚好被执行了,但由于while(br.readLine()!=null);已经结束了,process的输入流输出流还是没有被清空,到process.waitFor();时还是会造成堵塞的。
所以,一般我的做法是把上面的代码写到一个监视线程中,比如
class WatchThread extends Thread {
Process p;
boolean over;
public WatchThread(Process p) {
this.p = p;
over = false;
}
public void run() {
try {
if (p == null) return;
BufferedReader br = new BufferedReader(p.getInputStream());
while (true) {
if (p==null || over) {
break;
}
while(br.readLine()!=null);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void setOver(boolean over) {
this.over = over;
}
}
然后在process.waitFor()之前,添加
WatchThread wt = new WatchThread(process);
wt.start();
然后在process.waitFor()之后,添加
wt.setOver(true);
相关文章推荐
- C#调用Exe文件的方法及如何判断程序调用的exe已结束
- 将java类成为可执行文件.exe文件的一些方法、软件、下载地址
- Apache POI API操作Microsoft Office格式文件的方法+JAVA调用exe可执行文件
- 试了一下Java转成Exe可执行文件的几种方法
- Eclipse 开发java 制作exe可执行文件的方法
- java执行exe,bat 文件的方法
- C#调用Exe文件的方法及如何判断程序调用的exe已结束
- 将JAVA打包成EXE可执行文件的方法
- Matlab中去除exe执行时文件的DOS窗口的方法
- Java应用程序转化成exe文件的N种方法
- java 中调用window系统中的文件,或者执行命令(shell、.CMD、.EXE)并获取返回值
- Java桌面程序打包成exe可执行文件
- [置顶] java语言编程实现打开exe可执行文件以及txt,word等文件功能
- 利用JAVA执行本地EXE文件
- 手把手教你如何把java代码,打包成jar文件以及转换为exe可执行文件
- Java中class文件编译成exe文件的几种方法
- Java项目如何生成可执行jar包、exe文件
- 将Java的class文件转为EXE文件的八种方法
- 判断文件结束有两种方法:EOF和feof()
- [JAVA]利用bat将jar转变成一个可执行文件exe