Skip to content
Skip to breadcrumbs
Skip to header menu
Skip to action menu
Skip to quick search
Quick Search
Browse
Pages
Blog
Labels
Attachments
Mail
Advanced
What’s New
Space Directory
Feed Builder
Keyboard Shortcuts
Confluence Gadgets
Log In
Sign Up
Dashboard
Groovy
Copy Page
You are not logged in. Any changes you make will be marked as
anonymous
. You may want to
Log In
if you already have an account. You can also
Sign Up
for a new account.
This page is being edited by
.
Paragraph
Paragraph
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Preformatted
Quote
Bold
Italic
Underline
More colours
Strikethrough
Subscript
Superscript
Monospace
Clear Formatting
Bullet list
Numbered list
Outdent
Indent
Align left
Align center
Align right
Link
Table
Insert
Insert Content
Image
Link
Attachment
Symbol
Emoticon
Wiki Markup
Horizontal rule
tinymce.confluence.insert_menu.macro_desc
Info
JIRA Issue
Status
Gallery
Tasklist
Table of Contents
Other Macros
Page Layout
No Layout
Two column (simple)
Two column (simple, left sidebar)
Two column (simple, right sidebar)
Three column (simple)
Two column
Two column (left sidebar)
Two column (right sidebar)
Three column
Three column (left and right sidebars)
Undo
Redo
Find/Replace
Keyboard Shortcuts Help
<h2>Executing External Processes</h2><p>Groovy provides a simple way to execute command line processes. Simply write the command line as a string and call the <code>execute()</code> method. E.g., on a *nix machine (or a windows machine with appropriate *nix commands installed), you can execute this:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>def process = "ls -l".execute() println "Found text ${process.text}" </pre></td></tr></table><p>The <code>execute()</code> method returns a <code>java.lang.Process</code> instance which will subsequently allow the in/out/err streams to be processed and the exit value from the process to be inspected etc.</p><p>e.g. here is the same command as above but we will now process the resulting stream a line at a time</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>def process = "ls -l".execute() process.in.eachLine { line -> println line } </pre></td></tr></table><blockquote><p>in Groovy 1.5.5 the eachLine method is not available on InputStream. Earlier or later version will have that method. As an alternative you can use process.in.newReader().eachLine { line -> println line }</p></blockquote><p>Remember that many commands are shell built-ins and need special handling. So if you want a listing of files in a directory on a windows machine and if you write</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>Process p = "dir".execute() println "${p.text}" </pre></td></tr></table><p>you will get IOException saying "Cannot run program "dir": CreateProcess error=2, The system cannot find the file specified."</p><p>This is because "dir" is built-in to the windows shell (cmd.exe) and can't be run as a simple executable. Instead, you will need to write:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>Process p = "cmd /c dir".execute() println "${p.text}" </pre></td></tr></table><p>Also, because this functionality currently make use of <code>java.lang.Process</code> under the covers, the deficiencies of that class must currently be taken into consideration. In particular, the javadoc for this class says:</p><blockquote><p>Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock</p></blockquote><p>Because of this, Groovy provides some additional helper methods which make stream handling for processes easier.</p><p>Here is how to gobble all of the output (including the error stream output) from your process:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>def p = "rm -rf */*.tmp".execute() p.consumeProcessOutput() </pre></td></tr></table><p>There are also variations of <code>consumeProcessOutput</code> including one which takes output and error StringBuffers, another which takes output and error ditto for InputStreams and variations for StringBuffers, Writers and InputStreams which handle just the output stream or error stream individually. In addition, these is a <code>pipeTo</code> command (mapped to '<code>|</code>' to allow overloading) which lets the output stream of one process be fed into the input stream of another process.</p><p>Here are some examples of use:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>proc1 = 'ls'.execute() proc2 = 'tr -d o'.execute() proc3 = 'tr -d e'.execute() proc4 = 'tr -d i'.execute() proc1 | proc2 | proc3 | proc4 proc4.waitFor() if (proc4.exitValue()) print proc4.err.text else print proc4.text </pre></td></tr></table><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>def sout = new StringBuffer() def serr = new StringBuffer() proc2 = 'tr -d o'.execute() proc3 = 'tr -d e'.execute() proc4 = 'tr -d i'.execute() proc4.consumeProcessOutput(sout, serr) proc2 | proc3 | proc4 [proc2, proc3].each{ it.consumeProcessErrorStream(serr) } proc2.withWriter { writer -> writer << 'testfile.groovy' } proc4.waitForOrKill(1000) println 'sout: ' + sout println 'serr: ' + serr </pre></td></tr></table><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>def sout = new StringBuffer() def serr = new StringBuffer() proc1 = 'gzip -c'.execute() proc2 = 'gunzip -c'.execute() proc2.consumeProcessOutput(sout, serr) proc1 | proc2 proc1.consumeProcessErrorStream(serr) proc1.withWriter { writer -> writer << 'test text' } proc2.waitForOrKill(1000) println 'sout: ' + sout // => test text println 'serr: ' + serr </pre></td></tr></table><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>def initialSize = 4096 def outStream = new ByteArrayOutputStream(initialSize) def errStream = new ByteArrayOutputStream(initialSize) def proc = "ls.exe".execute() proc.consumeProcessOutput(outStream, errStream) proc.waitFor() println 'out:\n' + outStream println 'err:\n' + errStream </pre></td></tr></table><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>def out = new StringBuilder() def err = new StringBuilder() def proc = "ls".execute() proc.waitForProcessOutput(out, err) if (out) println "out:\n$out" if (err) println "err:\n$err" </pre></td></tr></table><h4>Further Information</h4><p>See also:</p><ul><li>Cookbook Examples: <a class="confluence-link" href="/display/GROOVY/Executing+External+Processes+From+Groovy" data-linked-resource-id="76412" data-linked-resource-type="page" data-linked-resource-default-alias="Executing External Processes From Groovy" data-base-url="http://docs.codehaus.org">Executing External Processes From Groovy</a></li><li>PLEAC <a href="http://pleac.sourceforge.net/pleac_groovy/processmanagementetc.html">Process Management</a> Examples</li><li>A Groovy Shell called <a class="confluence-link" href="/display/GROOVY/Groosh" data-linked-resource-id="17024" data-linked-resource-type="page" data-linked-resource-default-alias="Groosh" data-base-url="http://docs.codehaus.org">Groosh</a> which builds upon Groovy's basic processing functionality</li></ul>
Please type the word appearing in the picture.
Attachments
Labels
Location
Watch this page
< Edit
Preview >
Loading…
Save
Cancel
Next hint
search
attachments
weblink
advanced