日期:2014-05-20  浏览次数:20588 次

JAVA运行外部exe程序问题
exe程序文件夹:D:/res/report/Release/WordReportPrint.exe exe程序是一个C#写的windows应用程序,Release文件夹下面还有很多程序运行需要的dll。WordReportPrint.exe是一个查询数据库打印word报表的程序,需要传入一个参数。

我的代码:
Java code

// 启动cmd执行exe程序,args是传入参数
String command = "cmd /c start D:/res/report/Release/WordReportPrint.exe " + args;
// 执行
Process process = Runtime.getRuntime().exec(command);
// 等待进程完成
int bl = process.waitFor();
// 弹出下载文件选择框



结果:可以打印出word报表,但是process.waitFor()不起作用,原因应该是process.waitFor()判断的是cmd.exe的执行时间,但是那个时候WordReportPrint.exe还没有执行完成。所以下载失败。
后来我直接运行:
Java code

String path = "D:/res/report/Release/";
File dir = new File(path);
Runtime.getRuntime().exec("D:/res/report/Release/WordReportPrint.exe" + args,null,dir);


结果可以在process.waitFor()等待,但是程序执行不成功,不知道是不是找不到程序运行的dll文件。

请问怎么执行成功后,且能等待执行成功后执行下载操作。

------解决方案--------------------
程序执行是否依赖什么环境变量或路径(文件夹之类的),如果是,写成一个bat,再在java里调用,如果不是,直接
Process p = Runtime.getRuntime().exec("D:/res/report/Release/WordReportPrint.exe " + args);
p.waitFor();


------解决方案--------------------
探讨
exe程序文件夹:D:/res/report/Release/WordReportPrint.exe exe程序是一个C#写的windows应用程序,Release文件夹下面还有很多程序运行需要的dll。WordReportPrint.exe是一个查询数据库打印word报表的程序,需要传入一个参数。

我的代码:
Java code

// 启动cmd执行exe程序,args是传入参数
S……

------解决方案--------------------
感觉是不是C#用的环境变量没设置进去。