简体   繁体   English

Java 代码在作为独立的静态主类创建时工作正常,但在从 servlet 调用时不工作

[英]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.我正在使用 Tess4j API 来执行 OCR,并在 Eclipse 中创建了一个动态 Web 项目。 If I create a new java class directly under the Java resources folder, the code is working fine.如果我直接在 Java 资源文件夹下创建一个新的 Java 类,则代码工作正常。

    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.但是,当我从 Servlets doPost 方法调用相同的代码时,出现异常。

    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.我已经在 WEB-INF 的 lib 文件夹下包含了所有需要的 jars。 Have also added the jars in the projects build path.还在项目构建路径中添加了 jars。 Could anyone please let me know what I am doing wrong.任何人都可以让我知道我做错了什么。
Exception :例外 :
java.lang.IllegalStateException: Input not set java.lang.IllegalStateException:输入未设置
23:33:45.002 [http-bio-8080-exec-5] ERROR net.sourceforge.tess4j.Tesseract - Input not set 23:33:45.002 [http-bio-8080-exec-5] 错误 net.sourceforge.tess4j.Tesseract - 输入未设置
java.lang.IllegalStateException: Input not set java.lang.IllegalStateException:输入未设置

I think your current directory is different when you are calling from servlet.我认为当您从 servlet 调用时,您的当前目录是不同的。 the current directory is you tomcat bin folder.当前目录是你的 tomcat bin 文件夹。 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.您的 scan0008.jpg 必须放在 tomcat 的 bin 文件夹中,或者您必须使用文件的绝对路径。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 调用静态方法时会创建静态类变​​量-Java - Static class variables are created when Static methods are being called - Java 在Java中创建itext pdf:在从本地主机进行服务调用时,中文不起作用,但是在从主方法调用时,它可以正常工作 - itext pdf creating in java: Chinese not working when a service call is made from localhost but works fine when called from main method 为什么从另一个Java Servlet或Java类调用Java Servlet类中的方法会返回null? - Why method in Java servlet class return null when called from another java servlet or java class? 如果未在main方法中创建类的实例,是否会调用默认构造函数? - When an instance of a class is not created in the main method, will the default constructor be called? 从另一个类调用时,Java PropertyChangeListener不起作用 - Java PropertyChangeListener not working when called from another class 从不同的类调用时,Java repaint()无法正常工作 - Java repaint() not working when called from a different class java -verbose:class显示加载了servlet,但只有在访问servlet时才初始化静态内容 - java -verbose:class shows that the servlet is loaded but a static content is initialized only when the servlet is accessed 从独立java类调用时的单例类行为 - singleton class behaviour when calling from independent java classes 在主类中调用时,Netbeans Java接口不会显示 - Netbeans Java Interface won't display when called in the main class 什么时候调用java main方法? - When is java main method called?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM