我试图用Java启动vlc播放器,但不知何故它没有提示。 任何其他编程我尝试过。 PLZ看看我的代码:在java中启动vlc播放器
try {
Runtime.getRuntime().exec("K:\\...\\vlc.exe");
} catch (Exception ex) {
System.out.println(ex);
}
问题出在哪里开始VideoLAN的播放器?
我试图用Java启动vlc播放器,但不知何故它没有提示。 任何其他编程我尝试过。 PLZ看看我的代码:在java中启动vlc播放器
try {
Runtime.getRuntime().exec("K:\\...\\vlc.exe");
} catch (Exception ex) {
System.out.println(ex);
}
问题出在哪里开始VideoLAN的播放器?
您需要检查确保各种事情。
...只是我的路径。在我的真实项目中有\\ Programms \\ VideoLAN \\ VLC \\ 我不认为,我必须检查天气的文件是否存在,因为我将只启动Programm没有任何文件。 videoLAN Playe 1.0.x的作品! 0.9.x不要... – tryit 2009-11-13 19:29:23
事实上,你有一个错误,你不知道它是什么。我建议您将stderr
流与一个监听线程正确连接(至少!),以便您可以看到程序向您发送的错误消息。
要了解正确的步骤,请访问http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=4。参见清单4.5重定向out/err流。 – Thimmayya 2009-11-13 21:18:39
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();
}
}
}
}
实际你在你的代码中犯了一个错误,Runtime类的exec()方法返回java.lang.Process,所以你应该在你的代码中使用返回类型,所以试试这个代码...........
try {
process ps=Runtime.getRuntime().exec("K:\\...\\vlc.exe");
} catch (Exception ex) {
System.out.println(ex);
}
什么是例外? – 2009-11-13 19:25:09
也许尝试改述你的问题。 http://catb.org/~esr/faqs/smart-questions.html#beprecise – leonm 2009-11-13 22:47:23