-2

I am working on live streaming from blackmagic decklink card. So for that I need to execute a command.

String[] command={"./bmdcapture", "-m", "10", "-C", "0", "-V", "3", "-A", "2", "-F", "nut", "-f", "pipe:1", "|", "./avconv", "-i", "-", "-strict", "experimental", "-c:v", "libx264", "test.mp4"};
File f = new File("/home/NetBeansProjects/tools/card");
ProcessBuilder pb = new ProcessBuilder(command);
pb.directory(f);
pb.directory(f);
Process process = pb.start();

its works perfectly in terminal, But when i invoke via process-builder using java not working.

1 Answers1

1

OK, this is really a classic but I guess this still needs to be hammered into heads from time to time, and preferrably using a sledgehammer, and here the sledgehammer will be some bold font...

A ProcessBuilder does not run its processes via a command interpreter; as such, any command interpreter metacharacters, such as the * and the |, will be left untouched.

In your example, what you are basically trying to do is to pipe one command into another using a ProcessBuilder. But while this:

cmd1 | cmd2

will work in a Unix shell, this:

new ProcessBuilder("cmd1", "|", "cmd2")

will not. A ProcessBuilder has no idea what a pipe sign is and will just pass it as an argument to the command being issued (here, cmd1). This is the same behaviour you have with, for instance, execve() in the standard C library.

If you really want to pipe the output of a command into another you'll have to use two ProcessBuilders (one for each command) and redirect the output of the first command to the input of the second.

fge
  • 119,121
  • 33
  • 254
  • 329