How to get java getRuntime (). Exec () to run command line with arguments?

I am trying to write a java program that uses the Runtime.getRuntime().exec() method to use the command line to start an instance of the tesseract program.

Some background, Tesseract is a free, open source program that is used to perform OCR (optical character recognition) on images. It takes an image file and displays a text document. This is a command line program that uses this command to run

(from the command line shell)

 tesseract imageFilePath outFilePath [optional arguments] 

Example:

 tesseract "C:\Program Files (x86)\Tesseract-OCR\doc\eurotext.tif" "C:\Users\Dreadnought\Documents\TestingFolder\out" 

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 the output file. Tesseract only requires an output file name that does not require an extension.

Work on the command line works fine. However, I wanted to run this from a java program and ran into some errors.

I found this code very useful as a starting point

 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(); } } } 

It displays the result of the dir command. However, when I changed it like that

 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(); } } } 

The only thing it outputs is Exited with error code 1 . This is the expected result if the process failed.

I even tried to skip "cmd /c tesseract \"C:\\Program Files (x86)\\Tesseract-OCR\\doc\\eurotext.tif\" \"C:\\Users\\Dreadnought\\Documents\\TestingFolder\\eurotext-example\"" , and I got the same error.

According to Using quotes in getRuntime (). exec I thought the problem was that I was trying to avoid quotes, so I went into an array of strings. But I still get Exited with error code 1 .

Is it possible to run a command line program using the java Runtime.getRuntime().exec() ?


EDIT : The problem is still happening

I tried not to use the “cmd / c” thinking along the same line of reasoning as Yevgeny Dorofeev and Nandkumar Tekkale proposed below. However, I get another error:

 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.<init>(Unknown Source) at java.lang.ProcessImpl.start(Unknown Source) ... 4 more 

Maybe this gives more information? I'm really curious what causes this problem. Also the problem is the same, regardless of whether I add escaped quotes to my arguments.


EDIT 2 . On a whim, I provided an absolute path to the tesseract executable and did not use cmd /c , which worked like a charm. I think the question is, can Runtime.getRuntime().exec() not invoke environment variables?

+7
source share
4 answers

well tesseract is an external command, so you do not need to use it with cmd . Add tesseract to your environment variables. Use a direct command like:

 String[] commands = {"tesseract", imageFilePath, outputFilePath }; 

Exist status 1 means an invalid function. See process completion status

+1
source

Another workaround without having to recompile and deploy is to use the old DOS style paths, for example, C:\Program Files will be C:\Progra~1 . Of course, this will be useful only if you read the paths from the configuration file or the database and registry, etc.

0
source

another workaround is the full file installation path, for example / usr / local / Cellar / tesseract / 3.02.02 / bin / tesseract "

0
source

You do not capture STDERR, so if errors occur, you do not get them from STDOUT (which you capture). Try:

 BufferedReader input = new BufferedReader(new InputStreamReader( pr.getErrorStream())); 
0
source

All Articles