Java Runtime.exec() Linux Pipe

If you want to utilize Linux pipe advantages, you can do it in Java.

You may want to do the following, which is wrong.

String cmd = "ls"
Process p = Runtime.getRuntime().exec(cmd);

The correct way of executing shell command is the following:

String[] cmd = {
"/bin/sh",
"-c",
"ls"
};
 
Process p = Runtime.getRuntime().exec(cmd);
InputStream i = p.getInputStream();
byte[] b = new byte[16];
i.read(b, 0, b.length); 
System.out.println(new String(b));

In this example, the program just print out the strings. But in real use, you can feed it to some complicated process.

Category >> Concurrency >> Linux  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>