Launch vlc player in java

I tried to run the vlc player in Java, but for some reason these are not words. Any other Prog that I tried worked. Plz look at my code:

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

Where is the problem with starting PlayerLAN Player?

-2
source share
4 answers

The fact remains, you have a mistake, and you do not know what it is. Secondly, advice on the correct connection (at least!) Of the Stream stderrwith the listening stream, so that you see the error message that the program throws at you.

+1
source
  • Check if the path is valid (exists + this is a file)
  • Use more readable and portable path notation that uses slashes
  • stderr stdout , , OS- .

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

, .

0

You actually made a mistake in your code, the exec () method of the Runtime class returns java.lang.Process, so you should take the return type into your code, so try this code ...........

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

All Articles