Search code examples
javaeclipsetesseractruntime.exec

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


I'm trying to use tesseract to do OCR on an image in java. I realize there are wrappers like Tess4J that provide a bunch more functionality and stuff, but I've been struggling to get it set up properly. Simply running a one-line command with Runtime is really all I need anyways since this is just a personal little project and doesn't need to work on other computers or anything.

I have this code:

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

When it's compiled and run directly from terminal, it works perfectly. When I open the exact same file in eclipse, however, it gives an IOException:

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

What's going on? Thank you for any help.


Solution

  • Check the working folder in the run configuration for the Test class in Eclipse. I bet it's different from the one when you run the same program from a terminal.