0

出于某种原因,我在将输出发送到我用 Java 创建的进程时遇到问题。外部进程在命令提示符下运行,奇怪的是我可以点击它,输入,回车,然后我会从程序中得到输出。此外,我的程序可以读取来自程序的所有输出,它只是不能向它发送任何内容。

无论如何,这是我正在使用的相关代码,但它不起作用......

try {
    ProcessBuilder builder=new ProcessBuilder(args);
    builder.redirectErrorStream(true);
    final Process p=builder.start();
    // Process has been created and is running
    try {
        String b="";
        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
        final BufferedWriter output = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
        new Thread(){public void run(){
            // This thread will periodically send "get_time" to the process to get an update on its progress
            while(true)
            {
                try {
                    Thread.sleep(1000);
                    p.exitValue();
                    // p.exitValue() only works when process has ended, so normal code goes in the catch block
                    output.close();
                    break;
                    // Leave the infinite loop if the program has closed
                } catch (IOException ex) {
                    Logger.getLogger(OvMusicUI.class.getName()).log(Level.SEVERE, null, ex);
                    break;
                    // Leave the infinite loop if we tried closing our output stream, but it was already closed
                } catch (InterruptedException ex) {
                    Logger.getLogger(OvMusicUI.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalThreadStateException e) {
                    try {
                        System.out.println("Outputted: get_time");
                        output.write("get_time" + System.lineSeparator());
                        output.flush();
                        // Give the process some input
                    } catch (IOException ex) {
                        Logger.getLogger(OvMusicUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }}.start();
        while((b = input.readLine()) != null){
            System.out.println(new Time(System.currentTimeMillis()).toString() + " " + b);
            // Log all output the process gives
        }
        input.close();
    } catch (IOException ex) {
        Logger.getLogger(OvMusicUI.class.getName()).log(Level.SEVERE, null, ex);
    }
    // More code here
} catch (IOException ex) {
    Logger.getLogger(OvMusicUI.class.getName()).log(Level.SEVERE, null, ex);
}

如有必要,我可以给出一个示例命令和正在运行的外部程序的名称,以便您自己尝试...

谢谢!

编辑:这是传递给 ProcessBuilder 的示例:Arrays.asList("VLC\vlc.exe", "-Irc", "-vvv", "http://www.youtube.com/watch?v=xfeys7Jfnx8", "--sout", "file/ogg:Untitled 1.ogg", "--play-and-exit", "--rc-quiet")。唯一的区别是我使用绝对路径而不是相对路径。该程序是 VLC 媒体播放器 2.0.7。

4

1 回答 1

2

您的代码有一些问题。首先,您通常不应该常规控制流使用异常:它代价高昂,难以阅读,并且使处理实际错误更加困难。通常最好生成另一个Thread调用p.waitFor()并通知主线程完成的线程,例如使用wait/ notify

此外,您使用无限循环构造并使用break而不return会使您的代码更难以调试;相反,使用Timer.

看起来您的外部程序的输出可能工作正常,但问题只是读取其输出。该程序可能正在缓冲自己的输出,或者可能正在检测它没有以交互方式运行并且行为不同。

于 2013-07-31T18:57:49.713 回答