| <?xml version="1.0" encoding="UTF-8" standalone="no"?> |
| <!-- |
| Licensed to the Apache Software Foundation (ASF) under one |
| or more contributor license agreements. See the NOTICE file |
| distributed with this work for additional information |
| regarding copyright ownership. The ASF licenses this file |
| to you under the Apache License, Version 2.0 (the |
| "License"); you may not use this file except in compliance |
| with the License. You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, |
| software distributed under the License is distributed on an |
| "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| KIND, either express or implied. See the License for the |
| specific language governing permissions and limitations |
| under the License. |
| |
| --><mediawiki xmlns="http://www.mediawiki.org/xml/export-0.3/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="0.3" xml:lang="en" xsi:schemaLocation="http://www.mediawiki.org/xml/export-0.3/ http://www.mediawiki.org/xml/export-0.3.xsd"> |
| <siteinfo> |
| <sitename>NetBeans Wiki</sitename> |
| <base>http://wiki.netbeans.org/Main_Page</base> |
| <generator>MediaWiki 1.15.1</generator> |
| <case>first-letter</case> |
| <namespaces> |
| <namespace key="-2">Media</namespace> |
| <namespace key="-1">Special</namespace> |
| <namespace key="0"/> |
| <namespace key="1">Talk</namespace> |
| <namespace key="2">User</namespace> |
| <namespace key="3">User talk</namespace> |
| <namespace key="4">NetBeans Wiki</namespace> |
| <namespace key="5">NetBeans Wiki talk</namespace> |
| <namespace key="6">File</namespace> |
| <namespace key="7">File talk</namespace> |
| <namespace key="8">MediaWiki</namespace> |
| <namespace key="9">MediaWiki talk</namespace> |
| <namespace key="10">Template</namespace> |
| <namespace key="11">Template talk</namespace> |
| <namespace key="12">Help</namespace> |
| <namespace key="13">Help talk</namespace> |
| <namespace key="14">Category</namespace> |
| <namespace key="15">Category talk</namespace> |
| </namespaces> |
| </siteinfo> |
| <page> |
| <title>DevFaqOutputWindowExternalProcess</title> |
| <id>7437</id> |
| <revision> |
| <id>55907</id> |
| <timestamp>2012-07-17T21:47:15Z</timestamp> |
| <contributor> |
| <username>Javydreamercsw</username> |
| <id>2603</id> |
| </contributor> |
| <comment>Fix typo</comment> |
| <text xml:space="preserve">__NOTOC__ |
| ==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: |
| <pre>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(); |
| } |
| }</pre> |
| |
| |
| Create an ExecutionDescriptor: |
| <pre> |
| ExecutionDescriptor descriptor = new ExecutionDescriptor().controllable(true).frontWindow(true). |
| preExecution(new SomeRunnableToCallBeforeStart()).postExecution(new SomeRunnableToCallAfterExit()); |
| </pre> |
| |
| 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 <code>redirectErrorStream(true)</code> 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 <code>theCommandLineArguments</code> is an array of strings, just as you would pass to <code>Runtime.exec()</code> - the command-line to run whatever program you want to run. |
| |
| <pre>ExecutionService exeService = ExecutionService.newService( |
| new ProcessLaunch(theCommandLineArguments), |
| descriptor, "My Process"); |
| Future<Integer> exitCode = exeService.run();</pre> |
| |
| (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. |
| [[DevFaqOutputWindowExternalProcessNb67| 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: |
| |
| <pre> |
| 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)); |
| } |
| </pre> |
| |
| 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: |
| |
| <pre> |
| IOProvider.getDefault().getIO(name, false).getOut().println(mess); |
| </pre> |
| |
| The OutputHandler referenced above has been transformed into a plugin for easier use. See [http://plugins.netbeans.org/plugin/39695/?show=true here] for more details.</text> |
| </revision> |
| </page> |
| </mediawiki> |