2009-11-13 138 views
-2

我試圖用Java啓動vlc播放器,但不知何故它沒有提示。 任何其他編程我嘗試過。 PLZ看看我的代碼:在java中啓動vlc播放器

try { 
     Runtime.getRuntime().exec("K:\\...\\vlc.exe"); 
    } catch (Exception ex) { 
     System.out.println(ex); 
    } 

問題出在哪裏開始VideoLAN的播放器?

+1

什麼是例外? – 2009-11-13 19:25:09

+0

也許嘗試改述你的問題。 http://catb.org/~esr/faqs/smart-questions.html#beprecise – leonm 2009-11-13 22:47:23

回答

0

您需要檢查確保各種事情。

  1. 是否存在該文件(File.exists())。特別是高點(...)看起來不正確。 (或者它是一個省略號,你剛剛刪除了簡潔的路徑?)
  2. 它可執行嗎?
  3. 您需要從進程同時捕獲stdout/stderr,否則您會冒着進程阻塞的風險。更多信息this answer
+0

...只是我的路徑。在我的真實項目中有\\ Programms \\ VideoLAN \\ VLC \\ 我不認爲,我必須檢查天氣的文件是否存在,因爲我將只啓動Programm沒有任何文件。 videoLAN Playe 1.0.x的作品! 0.9.x不要... – tryit 2009-11-13 19:29:23

1

事實上,你有一個錯誤,你不知道它是什麼。我建議您將stderr流與一個監聽線程正確連接(至少!),以便您可以看到程序向您發送的錯誤消息。

+1

要了解正確的步驟,請訪問http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=4。參見清單4.5重定向out/err流。 – Thimmayya 2009-11-13 21:18:39

1
  1. 檢查的路徑是有效的(存在+是一個文件)
  2. 使用更具可讀性和便攜路徑符號,它使用斜槓
  3. 你必須讀出的標準錯誤和標準輸出流開始流程否則將掛在它特定的OS-bufffer充滿

Javacode:

import java.io.*; 
public class Test { 
    public static void main(String args[]) { 
    new Test("K:/Programms/VideoLAN/VLC/vlc.exe"); 
    } 

    public Test(String path) { 
    File f = new File(path); 
    if (!(f.exists()&&f.isFile())) { 
     System.out.println("Incorrect path or not a file"); 
     return; 
    } 
    Runtime rt = Runtime.getRuntime(); 
    try { 
     Process proc = rt.exec(path); 
     StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), false); 
     StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), false); 
     errorGobbler.start(); 
     outputGobbler.start(); 
     System.out.println("\n"+proc.waitFor()); 
    } catch (IOException ioe) { 
     ioe.printStackTrace(); 
    } catch (InterruptedException ie) { 
     ie.printStackTrace(); 
    } 
    } 
    class StreamGobbler extends Thread { 
    InputStream is; 
    boolean discard; 
    StreamGobbler(InputStream is, boolean discard) { 
     this.is = is; 
     this.discard = discard; 
    } 

    public void run() { 
     try { 
     InputStreamReader isr = new InputStreamReader(is); 
     BufferedReader br = new BufferedReader(isr); 
     String line=null; 
     while ((line = br.readLine()) != null) 
      if(!discard) 
      System.out.println(line);  
     } 
     catch (IOException ioe) { 
     ioe.printStackTrace(); 
     } 
    } 
    } 
} 
0

實際你在你的代碼中犯了一個錯誤,Runtime類的exec()方法返回java.lang.Process,所以你應該在你的代碼中使用返回類型,所以試試這個代碼...........

try { 
     process ps=Runtime.getRuntime().exec("K:\\...\\vlc.exe"); 
    } catch (Exception ex) { 
     System.out.println(ex); 
    }