-2

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

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

启动 videoLAN Player 的问题在哪里?

4

4 回答 4

1
  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();  
      }
    }
  }
}
于 2009-11-14T11:12:40.410 回答
1

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

于 2009-11-13T19:36:47.247 回答
0

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

  1. 该文件是否存在(File.exists())。特别是高音点 (...) 看起来是错误的。(或者它是一个省略号,你刚刚删除了简洁的路径?)
  2. 它是可执行的吗?
  3. 您需要同时从进程中捕获 stdout/stderr ,否则您将面临进程阻塞的风险。此答案的更多信息。
于 2009-11-13T19:25:03.607 回答
0

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

 try {
        process ps=Runtime.getRuntime().exec("K:\\...\\vlc.exe");
    } catch (Exception ex) {
        System.out.println(ex);
    }
于 2010-10-11T21:08:19.987 回答