[英]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.