用java启动vlc播放器
我试图用Java启动vlc播放器,但不知何故它并没有启动word。 我尝试过的任何其他程序都有效。 请看一下我的代码:用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的问题出在哪里?您需要检查并确保各种情况 该文件是否存在()。特别是那个高音点(…)看
try {
Runtime.getRuntime().exec("K:\\...\\vlc.exe");
} catch (Exception ex) {
System.out.println(ex);
}
启动videoLAN Player的问题出在哪里?您需要检查并确保各种情况
事实仍然是,你有一个错误,你不知道它是什么。我建议您(至少!)使用侦听线程正确连接
stderr
流,这样您将看到程序向您抛出的错误消息
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流。也许可以尝试重新表述您的问题。