English 中文(简体)
starting vlc player in java
原标题:

I tried to start vlc player in Java, but somehow it did not word. Any other Prog I tried worked. Plz have a look at my code:

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

Where is the problem starting videoLAN Player?

问题回答

The fact remains, you have an error and you don t know what it is. I second the advice to properly connect up (at least!) the stderr stream with a listening thread so you ll see the error message the program is throwing at you.

  1. Check if the path is valid (exists + is it a file)
  2. Use the more readable and portable path-notation which uses slashes
  3. You must read out the stderr and stdout streams of the started process else it will hang when the OS-specific bufffer for it is filled

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

You need to check ensure various things.

  1. does that file exists (File.exists()). In particular that treble dot (...) looks wrong. (or is it an ellipsis and you ve just removed the path for brevity ?)
  2. is it executable ?
  3. you need to capture stdout/stderr from the process concurrently, or you run the risk of the process blocking. More info with this answer.

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

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




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签