java 調用其他應用程序時,可能在windows下沒有問題,但是轉到linux下,卻會報這樣那樣的錯誤,比如有設計文件操作會報FileNotFoundException等等(如下代碼):
1 ProcessBuilder builder = new ProcessBuilder(); 2 builder.command(ffmpeg -i /data/download_video/20160425/20160425235938929164582_1.flv -y -vcodec libx264 -vpre ultrafast -vpre baseline -cqp 28 -coder 0 -refs 3 -deblockalpha 1 -deblockbeta -1 -me_method umh -subq 9 -me_range 32 -trellis 2 -chromaoffset -2 -nr 0 -b_strategy 1 -bframebias 0 -directpred 3 -g 250 -i_qfactor 1.3 -b_qfactor 1.4 -flags2 +bpyramid+wpred+mixed_refs+8x8dct -acodec libfaac -s 640*360 -b 532000 -ab 58000 -r 15 -pass 1 /data/video/output/20160425/20160425235938929164582_1_06400360.mp4); 3 builder.start();
4 ..........
原因: 不同的操作系統指令之間如果存在空格,可能會出現無法識別指令的錯誤。
解決方法: 指令中有空格的需要用不同的字符串分開,將指令用空格進行split,通過數組方式進行指令傳值,使用API:
public ProcessBuilder command(String... command)
可參考ProcessBuilder 類說明:
Constructs a process builder with the specified operating system program and arguments. This is a convenience constructor that sets the process builder's command to a string list containing the same strings as the command
array, in the same order. It is not checked whether command
corresponds to a valid operating system command.
- Parameters:
- command a string array containing the program and its arguments