title: Execute a command in Linux using Java and fetch the output
body: <p>I am using Groovy to execute commands on my Linux box and get back the output, but I am not able to use <code>|</code> pipes somehow (I think) or maybe it is not waiting for the command to finish.</p>
  
  <p>What is wrong or what am I missing in my code?</p>
  
  <p>My calling function:</p>
  
  <pre><code>def test()
  {
      String result="N"
  
      HashMap&lt;String,String&gt; params = IntermediateResults.get("userparams")
      Map env=AppContext.get(AppCtxProperties.environmentVariables)
  
      def fClass = new GroovyClassLoader().parseClass( new File( 'plugins/infa9/Infa9CommandExecUtil.groovy' ) )
      List&lt;String&gt; frows=["uname -a",
                          "uname -a | awk '{print\$2}'",
                          "uname -a | cut -d ' ' -f 2"]
      List&lt;String&gt; resultRows = fClass.newInstance().fetchCommandOutput( params, env, frows )
  
      return result
  }
  </code></pre>
  
  <p><code>Infa9CommandExecUtil.groovy</code> file content (update: added exitVal println):</p>
  
  <pre><code>package infa9
  
  import java.io.BufferedReader;
  
  public class Infa9CommandExecUtil {
    StringBuffer result
  
    public Infa9CommandExecUtil() {
      result = new StringBuffer()
    }
  
    public List&lt;String&gt; fetchCommandOutput( Map&lt;String,String&gt; params, Map env, List&lt;String&gt; rows )
    {
  
          List&lt;String&gt; outputRows = new ArrayList&lt;String&gt;()
          try
          {
              for(item in rows)
              {
                  String temp=item.toString()
                  println "CMD:$temp"
                  Process proc = Runtime.getRuntime().exec(temp);
                  InputStream stdin = proc.getInputStream();
                  InputStreamReader isr = new InputStreamReader(stdin);
                  BufferedReader br = new BufferedReader(isr);
                  String line = null;
  
                  result = new StringBuffer()
                  line=null
  
                  int exitVal = proc.waitFor()    //do I need to wait for the thread/process to finish here?
  
                  while ((line = br.readLine()) != null)
                  {
                      result.append(line+System.getProperty("line.separator"))    //to maintain the format (newlines)
                  }
                  String tRes=result
                  tRes=tRes.trim()
                  println "OUTPUT:$tRes\nEXITVAL:$exitVal"
  
                  outputRows.add(tRes)
              }
          }
          catch (IOException io)  {   io.printStackTrace();}
          catch (InterruptedException ie) {ie.printStackTrace();}
      return  outputRows
    }
  }
  </code></pre>
  
  <p>My output (update: added exitVal value):</p>
  
  <pre><code>CMD:uname -a
  OUTPUT:Linux estilo 2.6.18-128.el5 #1 SMP Wed Dec 17 11:41:38 EST 2008 x86_64 x86_64 x86_64 GNU/Linux
  EXITVAL:0
  CMD:uname -a | awk '{print$2}'
  OUTPUT:
  EXITVAL:1
  CMD:uname -a | cut -d ' ' -f 2
  OUTPUT:
  EXITVAL:1
  </code></pre>
  
  <p>Note: I am internally using <code>sh -c &lt;command&gt;</code>.</p>