Skip to content
Sebastian Schlicht edited this page Sep 18, 2013 · 6 revisions

Problem description

If you want to execute command line tools using Java you might use Runtime.exec(String) to create a new process emulating your command line call.

Considering you have a more complex call like

avconv -i test.mp3 -metadata title="B I B" -aq 100 -acodec libvorbis -vn testm.ogg

you could want to create a String exactly matching the one above and pass it to the Runtime.exec() method.

Process p = Runtime.getRuntime().exec("avconv -i test.mp3 -metadata title=\"B I B\" -aq 100 -acodec libvorbis -vn testm.ogg");

Unfortunately this approach will fail as the quotation marks will not be recognized correctly but you can leave them out neither.

If you have such a complex call I suggest to use ProcessBuilder and pass a list of Strings:
Do not add question marks to the list. Do not add spaces to the list. Just add all the options and arguments separately as shown above:

list.add("avconv");
list.add("-i");
list.add("test.mp3");
...
Process p = new ProcessBuilder(list).start();
Clone this wiki locally