java - How to make pipes work with Runtime.exec()? -
consider following code:
string commandf = "ls /etc | grep release"; try { // execute command , wait complete process child = runtime.getruntime().exec(commandf); child.waitfor(); // print first 16 bytes of output inputstream = child.getinputstream(); byte[] b = new byte[16]; i.read(b, 0, b.length); system.out.println(new string(b)); } catch (ioexception e) { e.printstacktrace(); system.exit(-1); }
the program's output is:
/etc: adduser.co
when run shell, of course, works expected:
poundifdef@parker:~/rabbit_test$ ls /etc | grep release lsb-release
the internets tell me that, due fact pipe behavior isn't cross-platform, brilliant minds work in java factory producing java can't guarantee pipes work.
how can this?
i not going of parsing using java constructs rather grep
, sed
, because if want change language, i'll forced re-write parsing code in language, totally no-go.
how can make java piping , redirection when calling shell commands?
write script, , execute script instead of separate commands.
pipe part of shell, can this:
string[] cmd = { "/bin/sh", "-c", "ls /etc | grep release" }; process p = runtime.getruntime().exec(cmd);
Comments
Post a Comment