我一直在试图写一个使用Java程序Runtime.getRuntime().exec()
方法来使用命令行运行程序“正方体”的一个实例。
一些背景下,超正方体是用于对图片执行OCR(光学字符识别)的自由开放源程序。 它发生在一个图片文件,输出的文本文档。 它是使用这个命令来运行一个命令行程序
(从命令提示外壳内)
tesseract imageFilePath outFilePath [optional arguments]
例:
tesseract "C:\Program Files (x86)\Tesseract-OCR\doc\eurotext.tif" "C:\Users\Dreadnought\Documents\TestingFolder\out"
第一个参数调用的Tesseract程序,二是图像文件的绝对路径和最后一个参数是输出文件应该是什么样的路径和名称。 正方体只需要它不需要扩展的输出文件的名称。
在命令提示工作这个完美的作品。 不过,我想从一个java程序运行此并运行到一些错误。
我这才发现这个代码是非常有益的出发点
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命令的结果。 然而,当我修改它像这样
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
。 这是预期的输出,如果过程因出错而结束。
我甚至试图通过"cmd /c tesseract \"C:\\Program Files (x86)\\Tesseract-OCR\\doc\\eurotext.tif\" \"C:\\Users\\Dreadnought\\Documents\\TestingFolder\\eurotext-example\""
我结束了具有相同的错误。
据中getRuntime使用引号()。exec的我认为问题是,我是,我曾试图逃跑的报价,所以这就是为什么我在一个字符串数组传递。 但我仍然得到Exited with error code 1
。
是否有可能执行与Java中的命令行程序Runtime.getRuntime().exec()
命令?
编辑 :该问题仍然存在的
我曾尝试不使用“CMD / C”在同一直线上的推理思维的叶夫根尼·Dorofeev和Nandkumar Tekale以下建议。 不过,我得到一种不同的错误:
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
也许这给更多信息? 我真的很好奇是什么原因造成了这个问题。 同样的问题是一样的我是否没有逃脱报价添加到我的论点。
编辑2:一时心血来潮我提供了一个绝对路径的Tesseract可执行文件,而不是使用cmd /c
的工作就像一个魅力。 我想这个问题是可以Runtime.getRuntime().exec()
不叫环境变量?