title: How to run infinte loop shell script from a java program
body: <p>I want to run a shell script from a java program. My shell script(loop_shell.sh) is working fine when I am running it in terminal and even my Java program(Execute.java) is working fine while running with other commands like 'ls' etc. But when I try run shell script from the Java program, shell script is not printing the values which are inside a loop. Here are the codes for both shell script and Java Program respectively.    </p>
  
  <p>loop_shell.sh:-</p>
  
  <pre><code> #bin/sh
  
  i=0
  
  j=0
  
  timer=0;
  
  echo "u r in loop_shell.sh"
  
  while((i!=1));
  
  do
  
   echo "waiting for the folder" 
  
  done
  </code></pre>
  
  <p>Execute.java:-</p>
  
  <pre><code>import java.io.BufferedReader;
  
     import java.io.IOException;
  
     import java.io.InputStream;
  
     import java.io.InputStreamReader;
  
     import java.io.File;
  
  
  
     public class Execute
     {
  
      public static void main (String args[])
      {
  
       String command="./loop_shell.sh";
  
       String output=executeCommand1(command);
  
       System.out.println(output);
      }
          public static String executeCommand1(String command) {
  
              StringBuffer output = new StringBuffer();
  
              Process p;
  
              try {
                  File dir = new File("/home/vamz/Desktop/sudhir_personal/JAVA_IMPORTANT/");//path
                  p = Runtime.getRuntime().exec(command,null,dir);
                  p.waitFor();
                  BufferedReader reader = 
                              new BufferedReader(new InputStreamReader(p.getInputStream()));
  
                              String line = "";           
                  while ((line = reader.readLine())!= null) {
                  output.append(line + "\n");
                  }
  
              } catch (Exception e) {
          e.printStackTrace();
          }
  
      return output.toString();
  
      }
  
    }
  </code></pre>
  
  <p>Output:- <code>u r in loop_shell.sh</code></p>
  
  <p>Expected Output:-  </p>
  
  <pre><code> u r in loop_shell.sh
   u r in loop_shell.sh
   u r in loop_shell.sh
   u r in loop_shell.sh
   u r in loop_shell.sh
   u r in loop_shell.sh
   ..........so on ....
  </code></pre>
  
  <p>If u try to run both the programs u will get the same output. U can see that output is just printing "u r in loop_shell.sh" and qutting with out waiting for shell script to complete!!
  Can some one pls explain me what is happening ? and pls tell me how to run infinte loop script from a java program.</p>