1

我有 Spring boot Webserver 项目,该项目在 Intellij IDEA 下的 PC 上运行正常,但在作为 war 文件分发到同一台 PC 后无法正常工作 - NoClassDefFoundError: Could not initialize class net.sourceforge.tess4j.TessAPI

我的代码:

ITesseract instance = new Tesseract(); // JNA Interface Mapping

 instance.setDatapath(new File(datapath).getPath()); 
 instance.setLanguage("eng");      
 try {
          String result = instance.doOCR(imageFile); 
 } catch (TesseractException e) {
          System.err.println(e.getMessage());
 }

我只有一个 Maven 依赖项,与 Tess4J 相关:

<dependency>
        <groupId>net.sourceforge.tess4j</groupId>
        <artifactId>tess4j</artifactId>
        <version>3.0.0</version>
</dependency>

运行分布式战争后出现错误:

There was an unexpected error (type=Internal Server Error, status=500).
Could not initialize class net.sourceforge.tess4j.TessAPI

完整的 Tomcat 日志:

java.lang.NoClassDefFoundError: Could not initialize class net.sourceforge.tess4j.TessAPI
        at net.sourceforge.tess4j.Tesseract.init(Tesseract.java:367) ~[tess4j-3.0.0.jar:3.0.0]
        at net.sourceforge.tess4j.Tesseract.doOCR(Tesseract.java:280) ~[tess4j-3.0.0.jar:3.0.0]
        at net.sourceforge.tess4j.Tesseract.doOCR(Tesseract.java:212) ~[tess4j-3.0.0.jar:3.0.0]
        at net.sourceforge.tess4j.Tesseract.doOCR(Tesseract.java:196) ~[tess4j-3.0.0.jar:3.0.0]
        at ocr.OCRController.handleFileUpload(OCRController.java:127) ~[classes/:0.3.0]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_51]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_51]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_51]
        at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_51]
        at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:222) ~[spring-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
        at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137) ~[spring-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
        at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) ~[spring-webmvc-4.2.3.RELEASE.jar:4.2.3.RELEASE]

如何修复错误?我计划在 Tomcat 下的 Windows Azure 中托管这个应用程序。感谢您的任何帮助,朋友!

4

3 回答 3

0

另一个修复方法NoClassDefFoundError是安装相应的Microsoft C++ Runtime(滚动到底部并展开Other Tools and Frameworks):

由于 DLL 是使用 Visual Studio 2015/2017 构建的,请确保您已安装 Visual C++ 2015 Redistributable 或 VC++ 2017 Redistributable。

有关详细信息,请参阅Tess4J开发教程。

于 2018-09-05T11:05:47.617 回答
0

我升级到 Windows 10,突然间 tessarct 不再适用于我的应用程序。

根据Tess4J文档,您需要安装VC++ 2017 Redistributable。

在我将它安装到 Windows 10 上之后,它又可以正常工作了。

如果您已升级到 Windows 10,并且您遇到了上述问题,并且之前也可以正常工作,那么这可能是您的问题。

于 2019-07-21T09:25:11.560 回答
0

The problem was in temp folder - it does not contain all necessary dlls.

Info for community: 1. Check temp folder under tomact - for me the path to it is: D:\Programs_Files\apache-tomcat-8.0.27\temp

This temp folder MUST contain the directory tess4j\win32-x86-64 with next 3 files: gsdll64.dll, liblept171.dll, libtesseract304.dll (or your version)

I had my problem because the directory tess4j\win32-x86-64 have never been created in temp.

BUT

My app works ok under Intellij IDEA because another Temp folder (for IDEA) works ok - C:\Users\Iuliia\AppData\Local\Temp\

contains tess4j\win32-x86-64\gsdll64.dll, tess4j\win32-x86-64\liblept171.dll, tess4j\win32-x86-64\libtesseract304.dll as expected.

Note, that this files are created in process of calling Tesseract in your code. Not early.

HOW I FIX THE PROBLEM with temp under Tomcat

I've added win32-x86-64\gsdll64.dll, win32-x86-64\liblept171.dll, win32-x86-64\libtesseract304.dll to the resources directory in my project.

Look at my project structure:

After deploying to tomcat, the necessary dlls is under classes directory (important!) - it means they are is in the scope.

Now it will be added to

D:\Programs_Files\apache-tomcat-8.0.27\temp\tess4j\win32-x86-64 as expected.

于 2016-01-29T21:51:56.623 回答