开发者

Command-line process not receiving anything from standard input

I have a command-lin开发者_StackOverflow中文版e application that waits for a keystroke to exit. The process is launching fine from Java, but when I send the keystroke (a), nothing happens. It looks like the application is never receiving anything from stdin. Here's a code sample:

Process p = Runtime.getRuntime().exec("\"C:\\app.exe\"");
Thread.sleep(5000);
OutputStream out = p.getOutputStream();
out.write(97);
out.flush();
out.close();
p.waitFor();

It works fine if I just execute it from the command-line. Does anyone know what the problem could be?

Thanks!


You need to read both streams, getInputStream and getErrorStream otherwise your program might block forever, see JavaDoc

The methods that create processes may not work well for special processes on certain native platforms, such as native windowing processes, daemon processes, Win16/DOS processes on Microsoft Windows, or shell scripts. The created subprocess does not have its own terminal or console. All its standard io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process through three streams (getOutputStream(), getInputStream(), getErrorStream()). The parent process uses these streams to feed input to and get output from the subprocess. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜