0

I'm trying to do OCR on PDFs using tess4j, which is working perfectly when I use a JUnit test to 'read' the PDF, but not when I run it using Primefaces FileUpload.

The listener calls the OCR class which does exactly the same as the JUnit test, except returning the string of the OCR-result. This is the java class:

import java.io.File;

import net.sourceforge.tess4j.Tesseract;

public class PDFToText
{

    public String doOCR(){
            try
            {
                Tesseract tess = new Tesseract();
                String text = "";

                imagePath = new File("D:/path/to/my.pdf");


                tess.setDatapath("D:/path/to/my/tessdata");
                text = tess.doOCR(imagePath);
                System.out.println(text);

                return text;
            } catch (Exception e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }
}

I read all those solutions here on SO, concerning JNA Version conflicts with com.sun.jna 3.0.9, etc.

But the never version (3.4.0) of tess4j, which I'm using, does not even include com.sun.java 3.9.0 anymore, it uses net.java.dev 4.1.0 Based on usages on the maven repository and also the source code inside the jar it looks like it's having the RESOURCE_PREFIX attribute.

Also did what @S.Isurika said here, which didn't help: Tess4j in glassfish ERROR:java.lang.NoSuchFieldError: RESOURCE_PREFIX , and anyway I don't want to place some jars in any directory as I'm working with maven (maven for eclipse).

I'm also using Selenium for my project, whose maven configuration depends on net.java.dev.jna version 3.4.0, but that doesn't look like a problem, cause it neither is missing RESOURCE_PREFIX in the Platform file.

First it seemed like a caching problem for me, but after I cleaned the project, cleaned maven, rebuilt maven's indexes, deleted my .m2 folder, deleted the deployment on GL, restarted eclipse and even the machine, I'm lost.

Thanks for your help!

Environment:

  • tess4j 3.4.0 (uses net.java.dev.jna 4.1.0.)
  • jai-imageio-core 1.3.1
  • java 1.8
  • maven-compiler-plugin 2.3.2
  • maven-war-plugin 2.3
  • Glassfish 4.0
  • Primefaces 6
  • Windows 7
  • myEclipse 2017

Here's my stacktrace:

Exception message:  
/templates/pages/secured/menu/page.xhtml @121,31 fileUploadListener="#{myBean.handleFiles}": java.lang.NoSuchFieldError: RESOURCE_PREFIX

Exception String:   
javax.faces.FacesException: 

/templates/pages/secured/menu/page.xhtml @121,31 fileUploadListener="#{myBean.handleAttachmentFiles}": java.lang.NoSuchFieldError: RESOURCE_PREFIX

Exception Cause:    
javax.el.ELException: /templates/pages/secured/menu/page.xhtml @121,31 fileUploadListener="#{mybean.handleAttachmentFiles}": java.lang.NoSuchFieldError: RESOURCE_PREFIX
Exception Class name:   

javax.faces.FacesException

Stack trace:    

javax.faces.FacesException: /templates/pages/secured/menu/page.xhtml @121,31 
fileUploadListener="#{mybean.handleAttachmentFiles}": java.lang.NoSuchFieldError: RESOURCE_PREFIX 
at com.sun.faces.lifecycle.ApplyRequestValuesPhase.execute(ApplyRequestValuesPhase.java:86) 
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198) 
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646) 
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682) 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318) 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160) 
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734) 
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673) 
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99) 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174) 
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260) 
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188) 
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191) 
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168) 
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119) 
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288) 
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AmeractIOStrategy.fireIOEvent(AmeractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AmeractThreadPool$Worker.doWork(AmeractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AmeractThreadPool$Worker.run(AmeractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:745) Caused by: javax.el.ELException: /templates/pages/secured/menu/page.xhtml @121,31 fileUploadListener="#{mybean.handleAttachmentFiles}": java.lang.NoSuchFieldError: RESOURCE_PREFIX
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:111)
at org.primefaces.component.fileupload.FileUpload.broadcast(FileUpload.java:319)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:755)
at javax.faces.component.UIViewRoot.processDecodes(UIViewRoot.java:931)
at com.sun.faces.lifecycle.ApplyRequestValuesPhase.execute(ApplyRequestValuesPhase.java:78) ... 30 more Caused by: java.lang.NoSuchFieldError: RESOURCE_PREFIX
at net.sourceforge.tess4j.util.LoadLibs.<clinit>(LoadLibs.java:63)
at net.sourceforge.tess4j.util.PdfUtilities.<clinit>(PdfUtilities.java:214)
at net.sourceforge.tess4j.util.ImageIOHelper.getIIOImageList(ImageIOHelper.java:392)
at net.sourceforge.tess4j.Tesseract.doOCR(Tesseract.java:212)
at net.sourceforge.tess4j.Tesseract.doOCR(Tesseract.java:196)
at com.me.utils.file.OCR.PDFToText.doOCR(PDFToText.java:121)
at com.me.proj.webLayer.requestScoped.mybean.handleAttachmentFiles(mybean.java:380)
at com.me.proj.webLayer.requestScoped.mybean$Proxy$_$$_WeldClientProxy.handleAttachmentFiles(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.el.parser.AstValue.invoke(AstValue.java:275)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) ... 34 more
danny
  • 358
  • 2
  • 14
  • 1
    SOLVED by workaround: Because glassfish uses its own old com.sun.jna package, it looks like the problem is there. As I cannot update the system, or transfer it to Wildfly (they use the newer net.java.dev.jna package), I had to use org.bytedeco.javacpp-presets tesseract implementation (https://github.com/bytedeco/javacpp-presets/tree/master/tesseract). – danny Jun 13 '17 at 13:11
  • See https://stackoverflow.com/questions/48275883/runtimeexception-when-trying-to-use-tess4j-in-java-ee/50165984#50165984 – nguyenq May 17 '18 at 20:58

0 Answers0