使用 Process.exec [英] using Process.exec

查看:77
本文介绍了使用 Process.exec的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的程序中间执行一个批处理文件(这将启动另一个 Java 应用程序).我不想等待或查看它是否成功执行,也不想从执行该批处理文件中捕获错误.启动该批处理文件后,我想做其他事情,而不是在执行该批处理后等待它.

I want to execute a batch file (this will start another java app) in the middle of my program. I dont want to wait or see whether it executed successfully nor I wanted to capture erros from executing that batch file. After I started that batch file , I want to do other stuff rather than waiting for it after i execute that batch.

我需要处理 stdout 和 stderr 吗?有什么办法可以摆脱对标准输出和标准错误的处理.

Do I need to take care of stdout and stderr? IS there any way to get rid of taking care of stdout and stderr.

这是我的第二篇文章,以消除我对这个主题的困惑,所以请具体针对问题,不要抛出如何处理 process.exe 或 processbuilder 的链接.

This is my second post to clear my confusion on this topic so please be specific to the question and just dont throw the link for how to process.exe or processbuilder.

感谢任何帮助.

推荐答案

简短回答:否.根据流程' javadocs,

Short answer: No. As per Process' javadocs,

由于一些原生平台只为标准的输入输出流提供有限的缓冲区大小,未能及时写入输入流或读取子进程的输出流可能会导致子进程阻塞,甚至死锁.

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.

因此,如果您希望您的程序远程健壮,您必须注意 stderr 和 stdout 处理.

So you have to take care of the stderr and stdout handling if you want your program to be remotely robust.

话虽如此,如果您真的不关心它们的内容(这是一个坏主意 - 如果批处理失败并显示您丢弃的有用错误消息怎么办?),您可以根据 Jay R. 的回答,触发线程来读取它们.这将使您的逻辑线程继续运行而无需担心流的状态,并且流食者将在后台运行,直到流耗尽.如果您发现自己经常这样做,您甚至可能想要围绕 Runtime.exec() 创建一个包装器,它会触发线程来为您执行此操作.

Having said that, if you really don't care about their content (which is a bad idea - what if the batch fails with a useful error message that you discard?), you can just fire off threads to read from them, as per Jay R.'s answer. This will let your logic thread continue without worrying about the state of the streams, and the stream eaters will run in the background until the streams are exhausted. You might even want to create a wrapper around Runtime.exec() that fires off threads to do this for you, if you find yourself doing this a lot.

但是,如果您不打算在代码中解释它,我至少会记录批处理的输出.当批次出现问题时,通过对流程的输出进行仔细分析,分析问题会容易得多.

I would, however, at least log the output from the batch process, if you're not going to intepret it in your code. When something goes wrong with the batch, analyzing the problem will be much easier with the process' output to pore over.

这篇关于使用 Process.exec的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆