Runtime.getRuntime().exec(“C:\\cygwin\\bin\\bash.exe”) has no input to read

走远了吗. 提交于 2019-12-01 10:43:12

Regardless of Java, as far as I know you can pipe output (or input) from/to bash only when it is running as a script, not when it is running as an interactive shell (in which case you can only pass cmd parameters to it).

In other words, when you run bash from cmd as you mention in the comment, you see output, but it is contained in the bash process, it is not output that bash sends back to the parent cmd process.

Regarding the javac process, it is actually sending the output to the error stream. Try running from cmd javac 1>null and javac 2>null and you'll see the difference.
Have you looked at the api here? You can try to use ProcessBuilder and redirect the error stream back to the primary input stream, it'll be much easier to work with the processes this way.

A process typically has not only one but two output streams associated with it. These are:

  1. stdout, which can be read with getInputStream()
  2. stderr, which can be read with getErrorStream()

Javac writes to stderr, not stdout, so you don't read its output.

Because it is inconvenient to have to read both of them (Some years ago, I had to write an extra thread for this), they introduced a new API to system processes, namely the ProcessBuilder, which allows to redirect stderr to stdout.

Just replace the lines

    Process proc = Runtime.getRuntime().exec(command);
    InputStream in = proc.getInputStream();

with

    ProcessBuilder pb = new ProcessBuilder(command);
    pb.redirectErrorStream(true);
    Process proc = pb.start();

, add the required imports, and your test succeeds :).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!