使用process.exec的JAVA Concurrent stdInput和stdError

时间:2015-05-19 20:29:56

标签: java multithreading process

我正在尝试在进程运行时从进程中读取stdInput和stdError。我创建了一个运行该进程的线程,这是有效的。在我的构造函数中传递对创建的进程的引用,以便我可以观察进程的输出。问题是它似乎没有链接引用进程,而是将其设置为null。所有这一切的目标是能够在进程仍在运行时观察进程的输出,目前我在查看输出完成后仍然困难。我认为这是由于胎面运行,但我希望有一种方法可以做到这一点。

    try { 
        Process p = null;
        Thread mythread =  new Thread( new SJ(pathF.getText(), fileF.getText(), p, view) ) ;
        mythread.start();
        BufferedReader stdInput = null;
        BufferedReader stdError = null;
        String s = null;
        stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
        stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
        while (mythread.isAlive()) { 
            while ((s = stdInput.readLine()) != null) { System.out.println(s); view.write( s + "\r\n" ); }
            while ((s = stdError.readLine()) != null) { System.out.println(s); view.write( s + "\r\n" ); }
        } 
    } catch ( IOException e ) {}



    class SJ implements Runnable {
        String Path = "";
        String File = "";
        public static viewering view;
        Process p = null;

        public SJ ( String arg1, String arg2, Process p1, viewering view )  {
            Path = arg1;
            File = arg2;
            p = p1;
        }

        public void run() {
            String[] command = new String[5];
            command[0] = "cmd";
            command[1] = "/C";
            command[2] = "compile";
            command[3] = Path;
            command[4] = File;

            BufferedReader stdInput = null;
            BufferedReader stdError = null;
            String s = null;
            try { 
                p = Runtime.getRuntime().exec( command, null, new File( "C:/Users/michael.b.goff/Documents/java/" ) );
                stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
                stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
                while ((s = stdInput.readLine()) != null) { System.out.println(s); view.write( s + "\r\n" ); }
                while ((s = stdError.readLine()) != null) { System.out.println(s); view.write( s + "\r\n" ); }
            } catch ( IOException e ) {}
        }
    }

以下是最终工作方案的代码

public void actionPerformed(ActionEvent ae) {
    frame.setVisible( false );
    final Thread mythread =  new Thread( new SJ(pathF.getText(), fileF.getText(), view) ) ;
    mythread.start();
    final Timer ThreadTimer = new Timer( 1000, null );
    ThreadTimer.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (!mythread.isAlive()) {
                frame.setVisible( true );
                ThreadTimer.stop();
            }
    }});
    ThreadTimer.start();
}

class SJ implements Runnable {
    String Path = "";
    String File = "";
    public static viewering view;

    public SJ ( String arg1, String arg2, viewering view1 )  { 
        Path = arg1;
        File = arg2;
        view = view1;
    }

    public void run() {
        String[] command = new String[5];
        command[0] = "cmd";
        command[1] = "/C";
        command[2] = "compile";
        command[3] = Path;
        command[4] = File;

        BufferedReader stdInput = null;
        BufferedReader stdError = null;
        Process p = null;
        String s = null;
        try { 
            p = Runtime.getRuntime().exec( command, null, new File( "C:/Users/michael.b.goff/Documents/java/" ) );
            stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
            stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
            while ((s = stdInput.readLine()) != null) { System.out.println(s); view.write( s + "\r\n" ); }
            while ((s = stdError.readLine()) != null) { System.out.println(s); view.write( s + "\r\n" ); }
        } catch ( IOException e ) {}
    }
}

1 个答案:

答案 0 :(得分:0)

好吧,那是一个完全不同的问题。现在我明白了你的真正问题。如果在按钮的单击侦听器中执行代码的第一部分,则在自己的线程中运行的Swings事件循环会挂起一段时间。然后你拥有来使用一个线程。 ;-)

看看这个问题的最后一个答案: Printing a Java InputStream from a Process