2009-11-13 63 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); 
    }