Java在线程中处理输入流
我开发了一个 Eclipse 插件,但遇到了一个问题
我的代码如下:
String run_pelda = "cmd.exe /C pelda.exe";
Runtime pelda_rt = Runtime.getRuntime();
Process pelda_proc = javacheckgen_rt.exec(run_pelda);
在我想读取输入流之后:
InputStream toolstr = tool_proc.getInputStream();
InputStreamReader r = new InputStreamReader(toolstr);
BufferedReader in = new BufferedReader(r);
但我的新 Eclipse 实例冻结了。我想我应该在java线程中完成它,但不幸的是我不知道如何正确使用它。
请给我一些想法!
I develop an Eclipse plugin and I have a problem
My code is the following one:
String run_pelda = "cmd.exe /C pelda.exe";
Runtime pelda_rt = Runtime.getRuntime();
Process pelda_proc = javacheckgen_rt.exec(run_pelda);
And after I would like to read the inputstream:
InputStream toolstr = tool_proc.getInputStream();
InputStreamReader r = new InputStreamReader(toolstr);
BufferedReader in = new BufferedReader(r);
But my new Eclipse instsnce freezes. I think I should do it in java threads, but unfortunatelly I don't know to use it correctly.
Please give me some ideas!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看看这篇优秀的文章 当 Runtime.exec() 不会 来自 JavaWorld 并查看是否有帮助。特别是,这可能是你的罪魁祸首:
本文提供了解决此问题的各种方法,包括在后台线程中使用
stderr
和stdout
的StreamGobbler
类的源代码。令人惊奇的是这篇文章的效果如此之好。它最初写于 2000 年,我发现其中几乎所有内容仍然准确。
Take a look the excellent article When Runtime.exec() won't from JavaWorld and see if it helps. In particular, this is probably your culprit:
The article provides various ways to address this problem, including the source code for a
StreamGobbler
class that consumesstderr
andstdout
in background threads.It's amazing how well this article has held up. It was originally written in 2000 and I find just about all of it to still be accurate.