vlc播放器 java,在Java中启动VLC播放器

本文档展示了在Java程序中尝试启动VLC播放器时遇到的问题及解决方案。问题在于程序可能因未正确处理进程的错误和输出流而导致挂起。提供的代码示例检查了文件路径的有效性,使用了更可读的路径表示,并通过读取并打印进程的错误和输出流防止程序挂起。
摘要由CSDN通过智能技术生成

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?

解决方案

Check if the path is valid (exists + is it a file)

Use the more readable and portable path-notation which uses slashes

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

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值