【问题标题】:starting vlc player in java在java中启动vlc播放器
【发布时间】:2010-12-16 10:07:13
【问题描述】:

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

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

启动 videoLAN Player 的问题在哪里?

【问题讨论】:

标签: java runtime inputstream


【解决方案1】:

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

  1. 该文件是否存在 (File.exists())。特别是高音点 (...) 看起来是错误的。 (或者它是一个省略号,而您刚刚删除了路径?)
  2. 是否可执行?
  3. 您需要同时从进程中捕获 stdout/stderr,否则您将面临进程阻塞的风险。更多信息this answer。

【讨论】:

  • ... 只是我的道路。在我的真实项目中有 \\Programms\\VideoLAN\\VLC\\ 我不认为,我必须检查文件是否存在,因为我只会在没有任何文件的情况下启动程序。 videoLAN Playe 1.0.x 有效! 0.9.x 不是...
【解决方案2】:

事实仍然存在,您有一个错误并且您不知道它是什么。我支持正确连接(至少!)stderr 流与侦听线程的建议,这样您就会看到程序向您抛出的错误消息。

【讨论】:

【解决方案3】:
  1. 检查路径是否有效(存在+是否为文件)
  2. 使用使用斜线的更具可读性和可移植性的路径表示法
  3. 您必须读出已启动进程的 stderr 和 stdout 流,否则当操作系统特定的缓冲区被填充时它将挂起

Java代码:

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();  
      }
    }
  }
}

【讨论】:

    【解决方案4】:

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

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

    【讨论】:

      猜你喜欢
      • 2011-08-18
      • 2014-01-19
      • 2012-02-19
      • 2022-01-20
      • 2011-03-08
      • 1970-01-01
      • 1970-01-01
      • 2016-08-31
      • 2021-05-09
      相关资源
      最近更新 更多