使用 Runtime.exec() 未完全执行 BAT 文件
我有一个 BAT 文件,它通过读取数据库表创建许多 csv 文件。 bcp.exe 用于此目的,因此,对于从表创建的每个 CSV,都有一个单独的 bcp.exe 调用。所有这些都可以在 BAT 文件中找到,我使用 Runtime.exec() 调用该文件。
现在我面临的问题是随机的。它无法在开发环境中重新创建,但在生产环境中偶尔会发生。
有时执行BAT文件后,只创建了少数CSV文件,其余的都丢失了。但是,当您重新执行相同操作时,您将获得所有 CSV。
这是代码:
String command = "cmd /C " + batFilePath + " " + batParams;
LOGGER.info("Executing : " + command);
Runtime rt = Runtime.getRuntime();
Process process = rt.exec(command);
process.getInputStream();
is = process.getInputStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
LOGGER.info(line);
}
如果有人能启发我这可能是如何发生的,我将非常感激,因为我对此一无所知。
预先感谢,
-Raj。
I have a BAT file, which creates a number of csv files by reading DB tables. bcp.exe is used for this purpose, thus, for each CSV created from a table, there's a separate bcp.exe call. All these are found in the BAT file, which I invoke using Runtime.exec().
Now the issue I face is random. It can't be recreated in developer environment, but occurs once in a while in the production environment.
Sometimes after the BAT file is executed, only few of the CSV files have been created, and the rest is missing. But when you re-execute the same, you get all the CSVs.
Here's the code:
String command = "cmd /C " + batFilePath + " " + batParams;
LOGGER.info("Executing : " + command);
Runtime rt = Runtime.getRuntime();
Process process = rt.exec(command);
process.getInputStream();
is = process.getInputStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
LOGGER.info(line);
}
Would really appreciate it if anyone can enlighten me on how this might happen, since I am all at sea regarding this.
Thanks in advance,
-Raj.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只是几点。
首先,我一直不明白为什么 Java 坚持使用 getInputStream 获取进程的输出流 - 这太奇怪了。但这只是我的咆哮,你对此无能为力:-)
其次,我不确定为什么你的代码中有一个“裸”的
process.getInputStream();
。我不认为这有什么坏处,但似乎没有必要。第三(说实话,这是我认为唯一可能有帮助的),您需要调试批处理文件本身而不是 Java 代码。
这可以通过以下两个建议来完成。
首先,获取错误流并查看它。
cmd
很可能正在提供您只是忽略的错误信息。其次,更改批处理文件以输出大量调试语句,如有必要,每行后输出一个。这有望将问题查明到批处理文件中的特定位置。
如果它只发生在生产中(并且间歇性地),那就更难了,但我们通常发现我们的客户非常愿意接受调试式的临时补丁,这样我们就可以收集信息来解决他们遇到的问题。
简单记录的批处理文件的输出也是低风险的更改。有些调试代码的风险并不低,我们必须在涉及客户生产系统之前非常彻底测试。有些人会直截了当地拒绝,这种立场并非不明智。
Just a couple of points.
The first is that I've never understood why Java insists on getting the process's output stream with
getInputStream
- that's just bizarre. But that's just me ranting, there's not much you can do about that :-)Secondly, I'm not sure why you have a "naked"
process.getInputStream();
in your code. I don't think it's bad but it seems unnecessary.Thirdly (and, to be honest, this is the only one I think may help), you need to debug the batch file itself rather than your Java code.
This can be done with the following two suggestions.
First, get the error stream and look at it. It's quite possible that
cmd
is delivering error information which you're just ignoring.Secondly, change the batch file to output copious amounts of debug statements, one after each line if necessary. This will hopefully pinpoint the problem down to a specific place in the batch file.
If it only happens in production (and intermittently), that's harder, but we generally find that our customers are more than willing to accept debug-style temporary patches so we can collect the information to fix the problems they're seeing.
Output from a batch file which is simply logged is also a low-risk change. Some debug code is not so low-risk and we have to test that very thoroughly before involving the customer production systems. Some will refuse point blank, a not-unwise position to take.
您可能在批处理脚本完成执行之前退出输入流代码。
之后:
您可能应该添加:
如果是这种情况,那么您可以通过故意减慢批处理脚本并检查您是否遇到问题来在开发人员环境中验证它。尝试将这样的内容粘贴
到批处理文件中。它将暂停您的脚本 5 秒钟。
It might be that you are exiting your input stream code before the batch script has completed executing.
After:
you should probably add:
If this is the case, then you could verify it in your developer environment by deliberately slowing down your batch script and checking whether you experience the problem. Try sticking something like this:
into your batch file. It will pause your script for 5 seconds.