运行时 exec 命令在 运行 来自终端但不是 eclipse 时工作

Runtime exec command working when when run from terminal but not eclipse

我正在尝试使用 tesseract 对 java 中的图像进行 OCR。我意识到有像 Tess4J 这样的包装器提供了更多的功能和东西,但我一直在努力正确设置它。 运行只需 运行 运行时的单行命令就是我所需要的,因为这只是一个个人小项目,不需要在其他计算机或任何东西上工作。

我有这个代码:

import java.io.IOException;

public class Test {
    public static void main(String[] args) {
        System.out.println(scan("full-path-to-test-image"));
    }
    public static String scan(String imgPath) {
        String contents = "";
        String cmd = "[full-path-to-tesseract-binary] " + imgPath + " stdout";
        try { contents = execCmd(cmd); }
        catch (IOException e) { e.printStackTrace(); }
        return contents;
    }
    public static String execCmd(String cmd) throws java.io.IOException {
        java.util.Scanner s = new java.util.Scanner(Runtime.getRuntime().exec(cmd).getInputStream()).useDelimiter("\A");
        return s.hasNext() ? s.next() : "";
    }
}

直接从终端编译 运行 后,它可以完美运行。但是,当我在 Eclipse 中打开完全相同的文件时,它会给出 IOException:

java.io.IOException: Cannot run program "tesseract": error=2, No such file or directory

这是怎么回事?感谢您的帮助。

检查 Eclipse 中测试 class 的 运行 配置中的工作文件夹。我敢打赌,当您从终端 运行 使用相同的程序时,它会有所不同。