0
votes

I am getting a NoClassDefFoundError on the PDDocument class when trying to read a PDF file with PDFBox. 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. I am not using Maven, still trying to learn how to use it. Netbeans version 11.0 Windows 10 Corretto OpenJDK 11.0.7

1
Which java version are you using? How are you running your code? Are you running it from an IDE or from a command line? - Abra
What IDE are you using? - Tilman Hausherr
I am using Corretto OpenJDK 11.0.7 in the Netbeans IDE version 11.0. I am running my command from the command line in windows 10. - Zugor
@Zugor in Netbeans, rightclick on the project, then Properties, then Libraries, then add pdfbox and fontbox jar there. (Assuming you don't use maven) - Tilman Hausherr
edit your question and post the command you use to run your java program. - Abra

1 Answers

1
votes

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. 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. Now my libraries are copied into the lib directory when I build the project and I no longer get errors.