2010-05-27 182 views
2

我的問題是關於apache commons中的org.apache.commons.exec.DefaultExecutor.execute(CommandLine命令)方法。從java執行命令行工具因命令行而異?

這是用於執行的ffmpeg的碼位:

command = FFMPEG_DIR + "ffmpeg -i \"" + file.getAbsolutePath() + "\""; 
DefaultExecutor executor = new DefaultExecutor(); 
ByteArrayOutputStream baos = new ByteArrayOutputStream(); 

PumpStreamHandler streamHandler = new PumpStreamHandler(baos); 
executor.setStreamHandler(streamHandler); 

CommandLine commandLine = CommandLine.parse(command); 

executor.execute(commandLine); 

當我執行的命令行工具E.C. ffmpeg的從Java這樣的:

/path_to_ffmpeg/ffmpeg -i "/My Media/Video/Day2/VIDEO.MOV" 

的ffmpeg的結果是,它無法找到輸入

"/My Media/Video/Day2/VIDEO.MOV": No such file or directory 

規定如果我在控制檯執行命令它工作沒有完全相同的方式文件任何問題。 將「我的媒體」文件夾重命名爲「MyMedia」可以解決Java端的問題,但對我來說這不是一個可用的解決方案。

如何解決這個問題,而不必限制輸入路徑的空間?

+0

你如何準備'CommandLine'? – aioobe 2010-05-27 19:24:10

+0

我添加了代碼 – itkevin 2010-05-27 20:08:09

回答

1

http://commons.apache.org/exec/tutorial.html的例子表明,你做這樣的事情:

DefaultExecutor de = new DefaultExecutor(); 
de.execute(CommandLine.parse("/path_to_ffmpeg/ffmpeg -i \"/My Media/Video/Day2/VIDEO.MOV\""); 
+0

我在執行之前解析了命令。同樣的問題 – itkevin 2010-05-27 19:33:13

+0

那麼如果你通過'addArgument'方法呢? – aioobe 2010-05-27 20:26:21

+0

同樣的結果。當我將工作目錄更改爲「/ My Media/Video/Day2」時,它適用於沒有空格的文件?!?我花了整整一天的時間,我無法弄清楚它:( – itkevin 2010-05-27 21:44:39