问题描述
我一直在尝试编写一个 Java 程序,该程序使用 Runtime.getRuntime().exec()
方法来使用命令行来运行程序tesseract"的实例.
I've been trying to write a java program that uses the Runtime.getRuntime().exec()
method to use the command-line to run an instance of the program "tesseract".
一些背景知识,Tesseract 是一个免费的开源程序,用于对图片执行 OCR(光学字符识别).它接收一个图片文件并输出一个文本文件.它是一个命令行程序,使用这个命令来运行
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
(来自命令提示符 shell)
(from within the command prompt shell)
tesseract imageFilePath outFilePath [optional arguments]
示例:
tesseract "C:Program Files (x86)Tesseract-OCRdoceurotext.tif" "C:UsersDreadnoughtDocumentsTestingFolderout"
第一个参数调用tesseract程序,第二个是图像文件的绝对路径,最后一个参数是输出文件的路径和名称.Tesseract 只需要输出文件的名称,不需要扩展名.
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.
在命令提示符下工作非常完美.但是,我想从 Java 程序中运行它并且遇到了一些错误.
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.
我发现这段代码作为起点非常有用
I found this this code to be very helpful as a starting off 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();
}
}
}
它打印出 dir 命令的结果.但是当我像这样修改它时
It prints out the result of the dir command. However when I modified it like so
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();
}
}
}
它唯一输出的是Exited with error code 1
.如果进程以错误结束,这是预期的输出.
The only thing it outputs is Exited with error code 1
. This is the expected output if the Process ended with an error.
我什至尝试过传递 "cmd/c tesseract "C:\Program Files (x86)\Tesseract-OCR\doc\eurotext.tif" "C:\Users\Dreadnought\Documents\TestingFolder\eurotext-example""
而我最终遇到了同样的错误.
I have even tried passing "cmd /c tesseract "C:\Program Files (x86)\Tesseract-OCR\doc\eurotext.tif" "C:\Users\Dreadnought\Documents\TestingFolder\eurotext-example""
and I ended up having the same error.
根据 在 getRuntime().exec 中使用引号,我认为问题在于我是因为我试图转义引号,所以这就是我传入 String 数组的原因.但我仍然收到 Exited with error code 1
.
According to Using Quotes within getRuntime().exec 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 Exited with error code 1
.
是否可以使用 java Runtime.getRuntime().exec()
命令执行命令行程序?
Is it possible to execute a command-line program with the java Runtime.getRuntime().exec()
command?
EDIT:问题仍然存在
我曾尝试不使用cmd/c",其思路与下面 Evgeniy Dorofeev 和 Nandkumar Tekale 建议的思路相同.但是,我收到了另一种错误:
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:
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 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.
EDIT 2:一时兴起,我提供了 tesseract 可执行文件的绝对路径,而不使用 cmd/c
就像一个魅力.我想问题是可以 Runtime.getRuntime().exec()
不调用环境变量吗?
EDIT 2: On a whim I provided an absolute path to the tesseract executable and not using the cmd /c
worked like a charm. I guess the question is can Runtime.getRuntime().exec()
not call environment variables?
推荐答案
您没有捕获 STDERR,因此当发生错误时,您不会从 STDOUT(您正在捕获)接收它们.试试:
You are not capturing STDERR, so when errors occur you do not receive them from STDOUT (which you are capturing). Try:
BufferedReader input = new BufferedReader(new InputStreamReader(
pr.getErrorStream()));
这篇关于如何让 java getRuntime().exec() 运行带参数的命令行程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!