在java代码中执行带参数的外部程序 [英] Executing in java code an external program that takes arguments
本文介绍了在java代码中执行带参数的外部程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Process p;
String line;
String path;
String[] params = new String [3];
params[0] = "D:\\prog.exe";
params[1] = picA+".jpg";
params[2] = picB+".jpg";
try
{
p = Runtime.getRuntime().exec(params);
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null)
System.out.println(line);
input.close();
}
catch (IOException e)
{
System.out.println(" procccess not read"+e);
}
我没有收到任何错误,只是没有。在cmd.exe中,prog.exe工作正常。
I don't get any error, just nothing. In cmd.exe prog.exe is working fine.
为了使这段代码有效,需要改进什么?
What to improve in order to make this code working?
推荐答案
也许您应该使用waitFor()来获取结果代码。这意味着标准输出的转储必须在另一个线程中完成:
Perhaps you should use waitFor() to obtain the result code. This means that the dump of the standard output must be done in another thread:
String path;
String[] params = new String [3];
params[0] = "D:\\prog.exe";
params[1] = picA+".jpg";
params[2] = picB+".jpg";
try {
final Process p = Runtime.getRuntime().exec(params);
Thread thread = new Thread() {
public void run() {
String line;
BufferedReader input =
new BufferedReader
(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null)
System.out.println(line);
input.close();
} catch (IOException e) {System.out.println(" procccess not read"+e);}
};
thread.start();
int result = p.waitFor();
thread.join();
if (result != 0) {
System.out.println("Process failed with status: " + result);
}
这篇关于在java代码中执行带参数的外部程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文