Tess4J error after distributing as war NoClassDefFoundError: Could not initialize class net.sourceforge.tess4j.TessAPI

Tess4J error after distributing as war NoClassDefFoundError: Could not initialize class net.sourceforge.tess4j.TessAPI

我有 Spring 引导 Web 服务器项目,它在我的 PC 中在 Intellij IDEA 下工作正常,但在分发到与 war 文件相同的 PC 后它不起作用 - NoClassDefFoundError:无法初始化 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>

我在 运行 我的分布式 war 之后得到了错误:

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 中托管此应用程序。感谢朋友们的帮助!

问题出在临时文件夹中 - 它不包含所有必需的 dll。

社区信息: 1. 检查 tomact 下的临时文件夹 - 对我来说它的路径是: D:\Programs_Files\apache-tomcat-8.0.27\temp

此临时文件夹必须包含目录 tess4j\win32-x86-64 以及接下来的 3 个文件:gsdll64.dll、liblept171.dll、libtesseract304.dll(或您的版本)

我遇到了问题,因为目录 tess4j\win32-x86-64 从未在临时文件中创建过。

但是

我的应用程序在 Intellij IDEA 下运行正常,因为另一个临时文件夹(用于 IDEA)运行正常 - C:\Users\Iuliia\AppData\Local\Temp\

按预期包含 tess4j\win32-x86-64\gsdll64.dll、tess4j\win32-x86-64\liblept171.dll、tess4j\win32-x86-64\libtesseract304.dll。

请注意,此文件是在您的代码中调用 Tesseract 的过程中创建的。不早。

如何解决温度低于 Tomcat

的问题

我已将 win32-x86-64\gsdll64.dll、win32-x86-64\liblept171.dll、win32-x86-64\libtesseract304.dll 添加到我项目的 resources 目录中。

看看我的项目结构:

部署到 tomcat 后,必要的 dll 位于 类 目录下(重要!)- 这意味着它们在范围内。

现在将添加到

D:\Programs_Files\apache-tomcat-8.0.27\temp\tess4j\win32-x86-64 如预期。

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

Since the DLLs are built using Visual Studio 2015/2017, please ensure you have Visual C++ 2015 Redistributable or VC++ 2017 Redistributable installed.

详情请参考Tess4J开发教程。

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

根据 Tess4J docs, you need to have VC++ 2017 Redistributable installed.

我在 Windows 10 上安装后,一切正常。

如果您已升级到 Windows 10,并且遇到上述问题,并且之前有效,那么这很可能是您的问题。