title: How to get java getRuntime().exec() to run a command-line program with arguments?
body: <p>I've been trying to write a java program that uses the <code>Runtime.getRuntime().exec()</code> method to use the command-line to run an instance of the program "tesseract".</p>
  
  <p>Some background, Tesseract is a free open source program that is used to perform OCR (Optical Character Recognition) on pictures. It takes in a picture file and outputs a text document. It is a command-line program that uses this command to run </p>
  
  <p>(from within the command prompt shell)</p>
  
  <pre><code>tesseract imageFilePath outFilePath [optional arguments] 
  </code></pre>
  
  <p>example:</p>
  
  <pre><code>tesseract "C:\Program Files (x86)\Tesseract-OCR\doc\eurotext.tif" "C:\Users\Dreadnought\Documents\TestingFolder\out"
  </code></pre>
  
  <p>the first argument calls the tesseract program, the second is the absolute path to the image file and the last argument is the path and name of what the output file should be. Tesseract only requires the name of the output file it does not require the extension.</p>
  
  <p>Working from the command prompt this works perfect. However, I was wanting to run this from a java program and was running into some errors.</p>
  
  <p>I found this this code to be very helpful as a starting off point</p>
  
  <pre><code>public class Main
  {
     public static void main(String args[])
     {
        try
        {
           Runtime rt = Runtime.getRuntime();
           String cmdString = "cmd /c dir";
  
           System.out.println(cmdString);
           Process pr = rt.exec(cmdString);
  
           BufferedReader input = new BufferedReader(new InputStreamReader(
                                                     pr.getInputStream()));
  
           String line = null;
  
           while ((line = input.readLine()) != null)
           {
              System.out.println(line);
           }
  
           int exitVal = pr.waitFor();
           System.out.println("Exited with error code " + exitVal);
  
        }
        catch (Exception e)
        {
           System.out.println(e.toString());
           e.printStackTrace();
        }
     }
  }
  </code></pre>
  
  <p>It prints out the result of the dir command. However when I modified it like so</p>
  
  <pre><code>public class Main
  {
     public static void main(String args[])
     {
        try
        {
           Runtime rt = Runtime.getRuntime();
           String imageFilePath = "\"C:\\Program Files (x86)\\Tesseract-OCR\\doc\\eurotext.tif\"";
           String outputFilePath = "\"C:\\Users\\Dreadnought\\Documents\\TestingFolder\\eurotext-example\"";
           String[] commands = {"cmd", "/c", "tesseract", imageFilePath, outputFilePath };
  
           Process pr = rt.exec(commands);
  
           BufferedReader input = new BufferedReader(new InputStreamReader(
                 pr.getInputStream()));
  
           String line = null;
  
           while ((line = input.readLine()) != null)
           {
              System.out.println(line);
           }
  
           int exitVal = pr.waitFor();
           System.out.println("Exited with error code " + exitVal);
        }
        catch (Exception e)
        {
           System.out.println(e.toString());
           e.printStackTrace();
        }
     }
  }
  </code></pre>
  
  <p>The only thing it outputs is <code>Exited with error code 1</code>. This is the expected output if the Process ended with an error.</p>
  
  <p>I have even tried passing <code>"cmd /c tesseract \"C:\\Program Files (x86)\\Tesseract-OCR\\doc\\eurotext.tif\" \"C:\\Users\\Dreadnought\\Documents\\TestingFolder\\eurotext-example\""</code> and I ended up having the same error.</p>
  
  <p>According to <a href="https://stackoverflow.com/questions/161859/using-quotes-within-getruntime-exec">Using Quotes within getRuntime().exec</a> I thought problem was that I was that i had tried to escape the quotes, so that is why I passed in a String array. But I am still getting the <code>Exited with error code 1</code>.</p>
  
  <p>Is it possible to execute a command-line program with the java <code>Runtime.getRuntime().exec()</code> command?</p>
  
  <hr>
  
  <p><strong>EDIT</strong>: The problem is still occuring</p>
  
  <p>I have tried not using "cmd /c" thinking along the same line of reasoning as Evgeniy Dorofeev and Nandkumar Tekale suggested below. However, I get a different sort of error:</p>
  
  <pre><code>java.io.IOException: Cannot run program "tesseract": CreateProcess error=2, The system cannot find the file specified
  java.io.IOException: Cannot run program "tesseract": CreateProcess error=2, The system  cannot find the file specified
      at java.lang.ProcessBuilder.start(Unknown Source)
      at java.lang.Runtime.exec(Unknown Source)
      at java.lang.Runtime.exec(Unknown Source)
      at Main.main(Main.java:15)
  Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
      at java.lang.ProcessImpl.create(Native Method)
      at java.lang.ProcessImpl.&lt;init&gt;(Unknown Source)
      at java.lang.ProcessImpl.start(Unknown Source)
  ... 4 more
  </code></pre>
  
  <p>Maybe this gives more information? I am really curious about what is causing this problem. Also the problem is the same whether or not I add the escaped quotations to my arguments.</p>
  
  <hr>
  
  <p><strong>EDIT 2</strong>: On a whim I provided an absolute path to the tesseract executable and not using the <code>cmd /c</code> worked like a charm. I guess the question is can <code>Runtime.getRuntime().exec()</code> not call environment variables?</p>