title: Why the command line utility hangs when invoked through java program?
body: <p>I need your suggestions and guidance in following task.
  I am using libdmtx which comes with a command line utility which reads the image files for  ECC200  Data  Matrix  barcodes, reads their contents, and writes the decoded messages to standard output. 
  I want to use this command line utility in my java program on linux platform. I amd using ubuntu linux. I have installed the libdmtx on my linux machine. and when I invoke the command </p>
  
  <p>dmtxread -n /home/admin/ab.tif</p>
  
  <p>on linux terminal it gives the decoded value of barcode in image immediately.</p>
  
  <p>when I am going to invoke this command using my java program the code stuks in execution of the command and dotn gives output.
  it looks like the program is processing or got hang.</p>
  
  <p>Following is my java code which invokes the following command</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.File;
  import java.io.IOException;
  import java.io.InputStreamReader;
  
  
  public class Classtest {
  
      public static void getCodes(){
  
          try 
          { 
              Process p; 
              String command[]=new String[3];
              command[0]="dmtxread";
              command[1]="-n";
              command[2]="/home/admin/ab.tif";
  
              System.out.println("Command : "+command[0]+command[1]+command[2]);
              p=Runtime.getRuntime().exec(command);  //I think hangs over here.
  
              BufferedReader reader=new BufferedReader(new InputStreamReader(p.getErrorStream()));
              String line=reader.readLine();
              if(line==null){
                  reader=new BufferedReader(new InputStreamReader(p.getInputStream()));
                  line=reader.readLine();
                  System.out.print("Decoded      :- "+line);
              }else{
                  System.out.print("Error      :- "+line);
              }
              System.out.println(p.waitFor());
  
          }catch(IOException e1) {
              e1.getMessage();
              e1.printStackTrace();
          }catch(InterruptedException e2) {
              e2.getMessage();
              e2.printStackTrace();
          } 
  
  
      }
      public static void main(String args[]){
          getCodes();
      }
  
  }
  </code></pre>
  
  <p>Please tell me friends where my code is going wrong.</p>
  
  <p>I refered to following article but dint get any help</p>
  
  <p><a href="http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=1" rel="nofollow">http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=1</a></p>
  
  <p>Please guide me friends!
  Thank you!</p>
  
  <p>Here is the new code in which I used the ProcessBuilder Class this code also giving the same output as above code that is it hangs at the line
  Process process = pb.start();</p>
  
  <pre><code>public class Test {
  
  public static void main(final String[] args) throws IOException, InterruptedException {
      //Build command 
      List&lt;String&gt; commands = new ArrayList&lt;String&gt;();
      commands.add("dmtxread");
      commands.add("-n");
      commands.add("/home/admin/ab.tif");
      System.out.println(commands);
  
      //Run macro on target
      ProcessBuilder pb = new ProcessBuilder(commands);
      pb.redirectErrorStream(true);
      Process process = pb.start();
  
      //Read output
      StringBuilder out = new StringBuilder();
      BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
      String line = null, previous = null;
      while ((line = br.readLine()) != null){
          System.out.println(line);
      }
  
      //Check result
      if (process.waitFor() == 0)
          System.out.println("Success!");
      System.exit(0);
  
      //Abnormal termination: Log command parameters and output and throw        ExecutionException
      System.err.println(commands);
      System.err.println(out.toString());
      System.exit(1);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Please guide me to solve this problem.
  Thanks You!</p>