简体   繁体   中英

Java code working fine when created as an independent static main class, but not when called from servlet

I am using Tess4j API for performing OCR and have created a dynamic web project in eclipse. If I create a new java class directly under the Java resources folder, the code is working fine.

    public static void main(String[] args){
        File image = new File("Scan0008.jpg");  
        ITesseract instance = new Tesseract();
        try{
            String result = instance.doOCR(image);
            System.out.println(result);
        }catch(TesseractException e){
            System.err.println(e.getMessage());
        }
    }

However I am getting an exception when I am calling the same code from my Servlets doPost method.

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            Validate valObj = new Validate();
            valObj.validate();
    }
    public void validate() {
        File image = new File("Scan0008.jpg");
        ITesseract instance = new Tesseract();
        try {
            String result = instance.doOCR(image);
            System.out.println(result);
        } catch (TesseractException e) {
            System.err.println(e.getMessage());
        }
    }

I have included all the required jars under lib folder of WEB-INF. Have also added the jars in the projects build path. Could anyone please let me know what I am doing wrong.
Exception :
java.lang.IllegalStateException: Input not set
23:33:45.002 [http-bio-8080-exec-5] ERROR net.sourceforge.tess4j.Tesseract - Input not set
java.lang.IllegalStateException: Input not set

I think your current directory is different when you are calling from servlet. the current directory is you tomcat bin folder. so when you are calling like this:

File image = new File("Scan0008.jpg");

your scan0008.jpg must be put in bin folder of tomcat or you must use absolute path of your file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM