Search code examples
javaexecnamed-pipes

Creating a named pipe in Java with exec calls


I'm trying to create a named pipe in Java with exec calls. I have this line of code

Process p = Runtime.getRuntime().exec("/bin/sh -c \"mkfifo ~/myFifo && tail -f ~/myFifo | csh -s\"");

But when I look for ~/myFifo after the call, it is not there. Is there any reason this would not work?

I also tried without the /bin/sh -c bit, but it didn't work either.

EDIT

final String [] cmds = {"/bin/sh", "-c", "\"mkfifo ~/myFifo && tail -f ~/myFifo | csh -s\""};

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

Solution

  • If you write out the third argument, you'll get this:

    "mkfifo ~/myFifo && tail -f ~/myFifo | csh -s"
    

    And if you try to run that in a shell, you'll see that it doesn't work:

    $ "mkfifo ~/myFifo && tail -f ~/myFifo | csh -s"
    bash: mkfifo ~/myFifo && tail -f ~/myFifo | csh -s: No such file or directory
    

    Just remove the literal quotes you added:

    final String [] cmds = {"/bin/sh", "-c", "mkfifo ~/myFifo && tail -f ~/myFifo | csh -s"};
    

    and remember to read from the Process.getInputStream().