简体   繁体   English

PDFBox NoClassDefFoundError PDDocument

[英]PDFBox NoClassDefFoundError PDDocument

I am getting a NoClassDefFoundError on the PDDocument class when trying to read a PDF file with PDFBox.尝试使用 PDFBox 读取 PDF 文件时,我在 PDDocument class 上收到 NoClassDefFoundError。 Here is the error I am getting:这是我得到的错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/pdfbox/pdmodel/PDDocument
        at readpdf.ReadPDF.main(ReadPDF.java:27)
Caused by: java.lang.ClassNotFoundException: org.apache.pdfbox.pdmodel.PDDocument
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
        ... 1 more

Here is the code that generates the error:这是生成错误的代码:

```
      try (PDDocument document = PDDocument.load(new File(pdfFile))) {
            PDFTextStripper textStripper = new PDFTextStripper();
            // Get total page count of the PDF document
            int numberOfPages = document.getNumberOfPages();
            //set the first page to be extracted
            textStripper.setStartPage(1);
            // set the last page to be extracted
            textStripper.setEndPage(numberOfPages);
            String text = textStripper.getText(document);
            System.out.println(text);
        }
```

Here are my imports just in case they could be an issue:这是我的进口,以防万一它们可能是一个问题:

```
import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
```

edit: Here is the command I use to run the program from the windows command window -> java -jar readPdf.jar pdfFile.pdf I added the following jar files as library files in Netbeans when I first started building the program, pdfbox-20.0.20.jar, fontbox-20.0.20.jar, and commons-logging-1.2.jar. edit: Here is the command I use to run the program from the windows command window -> java -jar readPdf.jar pdfFile.pdf I added the following jar files as library files in Netbeans when I first started building the program, pdfbox-20.0 .20.jar、fontbox-20.0.20.jar 和 commons-logging-1.2.jar。 I am not using Maven, still trying to learn how to use it.我没有使用 Maven,仍在尝试学习如何使用它。 Netbeans version 11.0 Windows 10 Corretto OpenJDK 11.0.7 Netbeans 版本 11.0 Windows 10 Corretto OpenJDK 11.0.7

The problem was the libraries were missing.问题是图书馆不见了。 For some reason Netbeans 11 was creating a library properties file and a directory called copy libs but not copying the libs to the directory nor was it creating a lib directory in the dist directory when I built the project.出于某种原因,Netbeans 11 正在创建一个库属性文件和一个名为 copy libs 的目录,但没有将这些库复制到该目录,也没有在我构建项目时在 dist 目录中创建一个 lib 目录。 I attempted to find the proper configuration but was unsuccessful.我试图找到正确的配置,但没有成功。 I finally downloaded version 12 of NetBeans and recreated the project from scratch and the problem disappeared.我终于下载了 NetBeans 的第 12 版并从头开始重新创建项目,问题就消失了。 Now my libraries are copied into the lib directory when I build the project and I no longer get errors.现在,当我构建项目时,我的库被复制到 lib 目录中,并且不再出现错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM