DevFaqOutputWindowExternalProcessJa

外部プロセスの出力を出力ウィンドウに表示させるにはどうしたらよいですか?

出力ウィンドウに書き込みをするにはトリックが必要です。まず、Runtime.exec() で外部プロセスを起動します。コードは GUI イベントスレッドで実行するようにしてはいけません。プロセスの標準出力、標準エラー出力をモニターするいくつかのスレッドが必要です。また出力ウィンドウにのパイプが必要です。サンプルコードは次のようになります:


public void launchProcess (String commandline) {
        if (EventQueue.isDispatchThread()) {
               throw new IllegalStateException ("Cannot be called from EDT");
        }
        final Process process = Runtime.getRuntime().exec (cmdline);

        //Get the standard out
        InputStream out = new BufferedInputStream (process.getInputStream(), 8192);
        //Get the standard in
        InputStream err = new BufferedInputStream (process.getErrorStream(), 8192);

        //Create readers for each
        final Reader outReader = new BufferedReader (new InputStreamReader (out));
        final Reader errReader = new BufferedReader (new InputStreamReader (err));

        InputOutput io = IOProvider.getDefault().getIO(scene.getName(), false);

        io.select();
        io.getOut().println(cmdline); //XXX
        io.getOut().println(); //XXXd

        OutHandler processSystemOut = new OutHandler (outReader, io.getOut());
        OutHandler processSystemErr = new OutHandler (errReader, io.getErr());

        //Get two different threads listening on the output & err
        RequestProcessor.getDefault().post(processSystemOut);
        RequestProcessor.getDefault().post(processSystemErr);

        try {
            //Hang this thread until the process exits
            process.waitFor();
        } catch (InterruptedException ex) {
            ErrorManager.getDefault().notify(ex);
        }
        processSystemOut.close();
        processSystemErr.close();
        if (process.exitValue() == 0) {
            //Get a localized string
            String successMessage = (NbBundle.getMessage(getClass(), "MSG_Success",
                outFile.getPath()));
            StatusDisplayer.getDefault().setStatusText (successMessage);
        } else {
            //Get a localized string
            String successMessage = (NbBundle.getMessage(getClass(), "MSG_Failure",
                outFile.getPath()));
            StatusDisplayer.getDefault().setStatusText (successMessage);
        }
}

    //The runnable that will handle an output stream, piping it to the output window:
    static class OutHandler implements Runnable {
        private Reader out;
        private OutputWriter writer;
        public OutHandler (Reader out, OutputWriter writer) {
            this.out = out;
            this.writer = writer;
        }

        public void run() {
            while (true) {
                try {
                    while (!out.ready()) {
                        try {
                            Thread.sleep(200);
                        } catch (InterruptedException e) {
                            close();
                            return;
                        }
                    }
                    if (!readOneBuffer() || Thread.currentThread().isInterrupted()) {
                        close();
                        return;
                    }
                } catch (IOException ioe) {
                    //Stream already closed, this is fine
                    return;
                }
            }
        }

        private boolean readOneBuffer() throws IOException {
            char[[ | ]] cbuf = new char[255];
            int read;
            while ((read = out.read(cbuf)) != -1) {
                writer.write(cbuf, 0, read);
            }
            return read != -1;
        }

        private void close() {
            try {
                out.close();
            } catch (IOException ioe) {
                ErrorManager.getDefault().notify(ioe);
            } finally {
                writer.close();
            }
        }
    }

課題 #57000 も参照してください。

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo