为什么我不能在java ee项目中使用PDDOCUMENT PDFBOX?

时间:2017-01-29 15:15:44

标签: java pdf java-ee text pdfbox

我正在尝试使用PDFBOX从pdf获取文本,正在开发一个maven jee项目,我已经包含了所有可能的dependecies但我仍然有下一个错误

`2017-01-29T16:01:50.069+0100|Avertissement: StandardWrapperValve[ViewCrous]: Servlet.service() for servlet ViewCrous threw exception
java.lang.NoClassDefFoundError: org/apache/pdfbox/pdmodel/PDDocument
    at ViewCrous.doGet(ViewCrous.java:27)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    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:416)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: org.apache.pdfbox.pdmodel.PDDocument
    at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1783)
    at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1633)
    ... 31 more

` 我正在使用的java代码是:

 `
//Loading the grill menu
          File file = new File("./Menu_GRILL.pdf");
          PDDocument document = PDDocument.load(file);

          //Instantiate PDFTextStripper class
          PDFTextStripper pdfStripper = new PDFTextStripper();

          //Retrieving text from PDF document
          String text = pdfStripper.getText(document);
          System.out.println(text);

          //Closing the document
          document.close();`

我的pom文件中的依赖项是:

`<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->
<dependency> 
         <groupId>org.apache.pdfbox</groupId> 
         <artifactId>pdfbox</artifactId> 
         <version>2.0.1</version> 
      </dependency>   

      <dependency> 
         <groupId>org.apache.pdfbox</groupId> 
         <artifactId>fontbox</artifactId> 
         <version>2.0.0</version> 
      </dependency>

      <dependency>  
         <groupId>org.apache.pdfbox</groupId> 
         <artifactId>jempbox</artifactId> 
         <version>1.8.11</version> 
      </dependency> 

      <dependency>
         <groupId>org.apache.pdfbox</groupId> 
         <artifactId>xmpbox</artifactId> 
         <version>2.0.0</version> 
      </dependency> 

      <dependency> 
         <groupId>org.apache.pdfbox</groupId> 
         <artifactId>preflight</artifactId> 
         <version>2.0.0</version> 
      </dependency> 

      <dependency> 
         <groupId>org.apache.pdfbox</groupId> 
         <artifactId>pdfbox-tools</artifactId> 
         <version>2.0.0</version> 
      </dependency>
<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>debugger-app</artifactId>
    <version>2.0.4</version></dependency>

需要你的帮助......

1 个答案:

答案 0 :(得分:1)

您在pom文件中使用了4个不同的版本(1.8.11,2.0.0,2.0.1,2.0.4)。使用“org.apache.pdfbox”删除所有内容并仅使用:

<dependency>
  <groupId>org.apache.pdfbox</groupId>
  <artifactId>pdfbox</artifactId>
  <version>2.0.4</version>
</dependency>

还要确保库及其依赖项位于war文件中。