Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用java启动vlc播放器_Java_Runtime_Inputstream - Fatal编程技术网

用java启动vlc播放器

用java启动vlc播放器,java,runtime,inputstream,Java,Runtime,Inputstream,我试图用Java启动vlc播放器,但不知何故它并没有启动word。 我尝试过的任何其他程序都有效。 请看一下我的代码: try { Runtime.getRuntime().exec("K:\\...\\vlc.exe"); } catch (Exception ex) { System.out.println(ex); } 启动videoLAN Player的问题出在哪里?您需要检查并确保各种情况 该文件是否存在()。特别是那个高音点(…)看

我试图用Java启动vlc播放器,但不知何故它并没有启动word。 我尝试过的任何其他程序都有效。 请看一下我的代码:

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

启动videoLAN Player的问题出在哪里?

您需要检查并确保各种情况

  • 该文件是否存在()。特别是那个高音点(…)看起来是错的。(或者它是一个省略号,您只是为了简洁而删除了路径?)
  • 它是可执行的吗
  • 您需要同时从流程中捕获stdout/stderr,否则您将面临流程阻塞的风险。更多信息与

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

  • 检查路径是否有效(存在+是否为文件)
  • 使用更具可读性和可移植性的路径表示法,它使用斜杠
  • 您必须读取已启动进程的stderr和stdout流,否则它将在操作系统特定的buffer填充时挂起
  • 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();  
          }
        }
      }
    }
    

    实际上,您在代码中犯了一个错误,运行时类的exec()方法返回java.lang.Process,所以您应该在代码中采用返回类型,所以请尝试以下代码

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

    ... 这只是我的路。在我的实际项目中有\\Programms\\VideoLAN\\VLC\\I不认为,我必须检查文件是否存在,因为我只会启动没有任何文件的程序。videoLAN Playe 1.0.x有效!0.9.x不是…要遵循的正确步骤位于。请参阅清单4.5以重定向out/err流。也许可以尝试重新表述您的问题。