Skip to content

DevFaqOutputWindowExternalProcess

Antonio Vieiro edited this page Jan 25, 2018 · 1 revision

DevFaqOutputWindowExternalProcess

How do I route the output from an external process to the output window?

NetBeans 6.8 and up: Use the External Execution API. Implement a Callable which will actually start the process:

private class ProcessLaunch implements Callable<Process> {
  private final String[] commandLine;
  public ProcessLaunch(String... commandLine) {
    this.commandLine = commandLine;
  }
  public Process call() throws Exception {
    ProcessBuilder pb = new ProcessBuilder(cmdline);
    pb.directory(new File(System.getProperty("user.home"))); //NOI18N
    pb.redirectErrorStream(true);
    return pb.start();
  }
}

Create an ExecutionDescriptor:

ExecutionDescriptor descriptor = new ExecutionDescriptor().controllable(true).frontWindow(true).
  preExecution(new SomeRunnableToCallBeforeStart()).postExecution(new SomeRunnableToCallAfterExit());

The before and after runnables can be used to, say, update the user interface when the process starts and stops.

Then actually launch your process. Standard output and standard error (if you leave in the call to redirectErrorStream(true) above) output will be redirected to the output window, and the tab name in the Output Window will be what you specify below. The variable theCommandLineArguments is an array of strings, just as you would pass to Runtime.exec() - the command-line to run whatever program you want to run.

ExecutionService exeService = ExecutionService.newService(
  new ProcessLaunch(theCommandLineArguments),
  descriptor, "My Process");
Future<Integer> exitCode = exeService.run();

(you can use the returned Future to wait for the process to exit and get its exit code - just don’t do that in the Swing event thread).

Applies to: NetBeans 6.8 and up. How to do this in NetBeans 6.7 and older

What about processes using the System.out/System.err?

Sometimes we are using a third party package that we don’t have access to its source or we don’t want to modify.

In this case you just need to redirect the system streams like this:

private void redirectSystemStreams() {
        out = new OutputStream() {

            @Override
            public void write(int i) throws IOException {
                OutputHandler.output(outputName, String.valueOf((char) i));
            }

            @Override
            public void write(byte[] bytes) throws IOException {
                OutputHandler.output(outputName, new String(bytes));
            }

            @Override
            public void write(byte[] bytes, int off, int len) throws IOException {
                OutputHandler.output(outputName, new String(bytes, off, len));
            }
        };
        System.setOut(new PrintStream(out, true));
        System.setErr(new PrintStream(out, true));
    }

OutputHandler is just a helper class that I’ve been using for a while. Feel free to use it. You need to add a dependency to I/O APIs package even if you don’t use it to avoid run time issues. If you don’t use it replace the OutputHandler calls for something like this:

IOProvider.getDefault().getIO(name, false).getOut().println(mess);

The OutputHandler referenced above has been transformed into a plugin for easier use. See here for more details.

Apache Migration Information

The content in this page was kindly donated by Oracle Corp. to the Apache Software Foundation.

This page was exported from http://wiki.netbeans.org/DevFaqOutputWindowExternalProcess , that was last modified by NetBeans user Javydreamercsw on 2012-07-17T21:47:15Z.

NOTE: This document was automatically converted to the AsciiDoc format on 2018-01-26, and needs to be reviewed.

Clone this wiki locally