2013-10-06 38 views
1

我想執行一個程序(System.getenv("appdata") + "ffmpeg")。我也希望能夠得到一個流程,或者可以讓我得到consle輸出的東西。我曾嘗試過「cmd/C」+ System.getenv("appdata") + "ffmpeg",它似乎沒有工作。任何幫助表示讚賞!使用Runtime.exec從另一個目錄執行程序

下面是一些代碼:

Process p = exec(testFFMpeg); 
    int ex = -1; 
    try { 
     ex = p.waitFor(); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
     CrashHandler.reportCrash("FFMpeg", "Unable to test FFMpeg", "start up with more permissions"); 
    } 

    if(ex == 0){ 
     System.out.println("Normal execution, exit value: " + ex); 
     BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); 
     String line = null; 

     do{ 
      try { 
       line = br.readLine(); 
       System.out.println(line); 
      } catch (IOException e) { 
       e.printStackTrace(); 
       CrashHandler.reportCrash("FFMpeg", "Unable to test FFMpeg", "start up with more permissions"); 
      } 
     }while(line != null); 
    }else{ 
     System.out.println("Execution exit value: " + ex); 
    } 
} 

private static Process exec(String[] cmd){ 
    try { 
     return Runtime.getRuntime().exec(cmd); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     CrashHandler.reportCrash("FFMpeg", "Unable to test FFMpeg", "start up with more permissions"); 
    } 

該文件的確切位置是:`System.getenv( 「應用程序數據」)+ 「\ VinVid \」 + 「ffmpeg.exe」。

+0

請看http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html –

+0

我會看看 – APerson

+0

有一件事是,我會如何得到輸出保存到文件。 – APerson

回答

1

我想通了。輸出進入錯誤流,而不是inputStream。

0

你缺少路徑分隔符,試試這個

System.getenv("appdata") + "\\ffmpeg" 
+0

它仍然不起作用 – APerson

+0

你能告訴我們你的錯誤嗎?你是否將擴展名'.exe'添加到ffmpeg? – Aneesh

+0

我試過Aneesh的回答,但增加了p.waitFor();剛剛創建BufferedReader後,它什麼都沒說。 – APerson

0

在你的代碼更改此行line = br.readLine();line += br.readLine(); 這是你如何運行在Java中的Windows進程。 我不知道你想要什麼輸出。如果您要求打印您運行的過程的輸出,此代碼有助於:

String line; 
String pidInfo =""; 

Process p =Runtime.getRuntime().exec(System.getenv("appdata") +"ffmpeg.exe"); 

BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); 

while ((line = input.readLine()) != null) { 
    pidInfo+=line; 
} 

input.close(); 

System.out.println(pidInfo); 
+0

它仍然不起作用 – APerson

+0

你會得到什麼錯誤? – Aneesh

+0

根本沒有輸出 – APerson

相關問題