Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoke a unix shell from java program?read and write a steady stream of data to and from a unix pipe from java program

I have 2 questions

1) how do I i invoke a unix shell from a java.runtime library to run a command like this

Process p = Runtime.getRuntime().exec(commands);

cat alias > bias

2) How can I read and write to a unix pipe a steady stream of data from java. Do i have to make all the system calls like open read write to a pipe

I basically want to replicate this command

cat alias > bias

where the steady stream of data will be come from java program to bias and not cat alias.

like image 233
ssD Avatar asked Dec 28 '22 05:12

ssD


1 Answers

Since JDK7, there is a convinience way to do this:

ProcessBuilder pb = new ProcessBuilder("cat", "alias");
File bias = new File("bias");
pb.redirectOutput(Redirect.appendTo(bias));
pb.start();

Reference: ProcessBuilder#redirectOutput

like image 197
Isaiah Avatar answered Dec 30 '22 19:12

Isaiah