我试图在 Java 中启动 vlc 播放器,但不知何故它没有说话。我尝试过的任何其他 Prog 都有效。请看看我的代码:
try {
Runtime.getRuntime().exec("K:\\...\\vlc.exe");
} catch (Exception ex) {
System.out.println(ex);
}
启动 videoLAN Player 的问题在哪里?
我试图在 Java 中启动 vlc 播放器,但不知何故它没有说话。我尝试过的任何其他 Prog 都有效。请看看我的代码:
try {
Runtime.getRuntime().exec("K:\\...\\vlc.exe");
} catch (Exception ex) {
System.out.println(ex);
}
启动 videoLAN Player 的问题在哪里?
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();
}
}
}
}
事实仍然存在,你有一个错误,你不知道它是什么。我支持使用监听线程正确连接(至少!)stderr
流的建议,这样您就会看到程序向您抛出的错误消息。
你需要检查确保各种事情。
实际上你在你的代码中犯了一个错误,Runtime 类的 exec() 方法返回 java.lang.Process 所以你应该在你的代码中使用返回类型所以试试这个代码............
try {
process ps=Runtime.getRuntime().exec("K:\\...\\vlc.exe");
} catch (Exception ex) {
System.out.println(ex);
}