在java中启动vlc播放器

时间:2009-11-13 19:22:35

标签: java runtime inputstream

我尝试用Java启动vlc播放器,但不知怎的,它没有说出来。 我尝试过的任何其他Prog都有效。 Plz看看我的代码:

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

启动videoLAN Player的问题在哪里?

4 个答案:

答案 0 :(得分:1)

事实仍然是,你有一个错误,你不知道它是什么。我的建议是将stderr流与监听线程正确连接(至少!),这样你就会看到程序向你抛出的错误信息。

答案 1 :(得分:1)

  1. 检查路径是否有效(存在+是文件)
  2. 使用更易读,更便携的使用斜杠
  3. 的路径表示法
  4. 你必须读出已启动进程的stderr和stdout流,否则它将在为其填充特定于操作系统的缓冲区时挂起
  5. 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();  
          }
        }
      }
    }
    

答案 2 :(得分:0)

你需要检查确保各种事情。

  1. 该文件是否存在(File.exists())。特别是高音点(......)看起来不对劲。 (或者它是省略号,你刚刚删除了简洁的路径?)
  2. 可执行吗?
  3. 您需要从进程并发中捕获stdout / stderr,否则您将面临进程阻塞的风险。有关this answer的更多信息。

答案 3 :(得分:0)

实际上你在代码中犯了一个错误,runtime类的exec()方法返回java.lang.Process所以你应该在你的代码中使用一个返回类型,所以试试这个代码...........

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