title: Execute shell script in Java and read Output
body: <p>I am executing the shell script using below method in java </p>
  
  <pre><code>public static void main(String ar[])
  {
    //key value are being read from properties file, here I am assigning the sample values      directly
  
     key=mine
     value="ls-1|tail-1"
  
  
    String[] cmd = { "jj.sh" , key,value};
    Process script_exec = Runtime.getRuntime().exec(cmd);
    script_exec.waitFor();
    if(script_exec.exitValue() != 0){
     System.out.println("Error while executing script");
  
    BufferedReader stdInput = new BufferedReader(new
                  InputStreamReader(script_exec.getInputStream()));
  
    while ((s = stdInput.readLine()) != null) {
                  System.out.println(s);
              }
  }
  </code></pre>
  
  <p>The jj.sh file contains below value</p>
  
  <pre class="lang-sh prettyprint-override"><code>#!/bin/sh
  
  key=$1
  value=`$2` 
  echo $value
  </code></pre>
  
  <p>When I execute jj.sh using key and value directly, it give me the correct value, that is, the name of file. 
  However with java it is giving me the result as ls -1 result (Means java is ignoring the command after '|'). When I pass the key values  delimited with tilde sign &#96; then it simply displays the full key value i.e. <code>ls -1|tail -1</code></p>
  
  <p>How to execute the full command using java</p>