ProcessBuilder misbehaves with a few arguments

I have a ProcessBuilder:

String src = c:/hello/ String dst = c:/hello/2 ProcessBuilder builder = null; builder = new ProcessBuilder("c:/file/file.exe", "-i", src, "-f", "-l 500", dst); builder.redirectErrorStream(true); process = builder.start(); 

The problem is that as soon as I add "-l 500" , I get the output:

"l 500" invalid command

Although I entered "-l 500" and not "l 500" . If I enter "--l 500" , I get:

"- l 500" invalid command

Even if -l 500 is a valid command when run at a command prompt.

If I REMOVE "-l 500" , it works again.

Am I using Processbuilder incorrectly?

EDIT:

It seems good that it works if I do "-l" and "500" as separate entries:

 new ProcessBuilder("c:/file/file.exe", "-i", src, "-f", "-l", "500", dst); 

Why is this so? Can I have a command with a space in it as the same record?

+4
source share
2 answers

When you run it on the command line, you don't wrap the -l 500 in quotation marks, so they are treated as two different arguments. Enter this on the command line:

file.exe -i some_source -f "-l 500" some_dest

and I expect that you will see the same error message that you see when ProcessBuilder used incorrectly. The file.exe program should parse the command line, looking for lines with the leading character - . When it finds the single line "-l 500" , it deletes - and does not recognize l 500 as a valid argument.

The argument for ProcessBuilder similar to the quoted argument on the command line.

+4
source

I ran into the same problem with the ffmpeg command, where I have many parameters with values. I ended up creating an ArrayList and adding each item to the list one by one. Here is an example:

 List<String> command = new ArrayList<>(); command.add(ffmpegCommand); command.add("-re"); command.add("-i"); command.add(videoFile); command.add("-vcodec"); command.add("libx264"); command.add("-vcodec"); command.add("libx264"); command.add("-vb"); command.add("500000"); command.add("-g"); command.add("60"); command.add("-vprofile"); command.add("main"); command.add("-acodec"); command.add("aac"); command.add("-ab"); command.add("128000"); command.add("-ar"); command.add("48000"); command.add("-ac"); command.add("2"); command.add("-vbsf"); command.add("h264_mp4toannexb"); command.add("-strict"); command.add("experimental"); command.add("-f"); command.add("mpegts"); command.add("udp://127.0.0.1:10000?pkt_size=1316"); ProcessBuilder pb = new ProcessBuilder(command); pb.redirectErrorStream(true); Process process; try { process = pb.start(); process.waitFor(); if (process.exitValue() == 0) { // success } else { // failure } } catch (IOException | InterruptedException e) { // handle exception } 

Where ffmpegCommand is the full path to the command, and videoFile is the full path to the video. This is the only way I was able to run the command successfully.

+2
source

All Articles