简体   繁体   中英

Writing to the OutputStream of Java Process/ProcessBuilder as pipe

I have problems sending data from java to a (linux)-subprocess created by ProcessBuilder/Process .

A shell-only based basic example would look like as follows and works fine.

echo "hello world" | cat - >/tmp/receive.dat

Now, I want to substituted the echo "hello world" by a java program which should internally create a new process (the cat - >/tmp/receive.dat ) and then send data to it.

I tried the following, but the file /tmp/receive.dat remains untouched:

String[] cmdArray = { "/bin/cat", "-", ">/tmp/receive.dat" };
ProcessBuilder builder = new ProcessBuilder (cmdArray);
builder.directory (new File ("/tmp"));
Process p = builder.start ();
OutputStream pos = p.getOutputStream ();

byte [] bytes = "hello world".getBytes ();
pos.write (bytes);
pos.close ();
p.waitFor ();

The same happens under Windows, of course with an adapted cmdArray:

cmd /c type con >c:\tmp\receive.dat

Printing directly to system.out from java is no alternative as many subprocesses should be called within the livecycle of the java program.

thx for any help! Tombo

The issue here is that /bin/cat does not actually write to files, it merely writes to standard out. The output redirection >/tmp/receive.dat is actually performed by the shell, but you are bypassing the shell by invoking cat in this manner.

If what you are trying to achieve is merely an OutputStream that writes to a file, then doing that via standard java I/O (eg, FileOutputStream and friends) is what you want. It is also cross-platform (ie, Windows-friendly), unlike anything that depends on the shell.

Regarding the comment about not being able to merely write to standard out from java because of subprocesses - any subprocess you invoke can inherit standard out from their parent process (the java process - look at ProcessBuilder.Redirect.INHERIT ). So even if you are invoking subprocesses from java, redirecting all the output to a file should still work in the same way as your initial example (where the java program replaces the echo command).

您可能想要ProcessBuilder#redirectOutput(File) ,因为>功能不是cat的,而是所谓的cat(在我们的意义上,是流程生成器)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM