Re: exec() and sending to STDIO and reading from STDIO
Gordon Beaton wrote:
On 18 Oct 2006 00:46:36 -0700, WinstonSmith_101@hotmail.com wrote:
Hello thanks for the feedback, but this merely echos out "foo | gurka".
No, it doesn't. Post your *actual* code. You are not running a shell
as I suggested.
This is what happens when I run it with real commands:
bash$ cat Test.java
import java.io.*;
public class Test {
public static void main(String[] args) throws Exception {
String[] cmd = { "/bin/sh", "-c", "echo this is a test | wc" };
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader br = new BufferedReader(new InputStreamReader (p.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}
}
base$ javac Test.java
bash$ java Test
1 4 15
bash$
The example is complete and compilable. Try it.
- your example works. But when I substitude the program gpg for wc, I
stop getting the output. Can it be that it sends on STDERR or
something?
import java.io.*;
public class Test1
{
public static void main(String[] args) throws Exception
{
String[] cmd = { "/bin/sh", "-c", "echo mypasswd | gpg
--passphrase-fd 0 -o tobe.zip -q --decrypt tobe.zip.gpg" };
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader br = new BufferedReader(new InputStreamReader
(p.getInputStream()));
String line;
while ((line = br.readLine()) != null)
{
System.out.println("XXX: " + line);
}
br.close();
}
}
- it's the same. I just changed the "String[] cmd =" line