java.lang.ClassNotFoundException: org.apache.pdfbox.multipdf.PDFMergerUtility

I try to combine mutliple PDFs into one using PDFBox. I was following this tutorial from tutorialpoint. But whenever I run it (java -jar pdftool.jar ./pdf-files/*) it throws the following error. Following is the main.java file and the pom.xml for maven.

error

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/pdfbox/multipdf/PDFMergerUtility         at ch.jocomol.lw.Main.main(Main.java:14) Caused by: java.lang.ClassNotFoundException: org.apache.pdfbox.multipdf.PDFMergerUtility         at java.net.URLClassLoader.findClass(URLClassLoader.java:382)         at java.lang.ClassLoader.loadClass(ClassLoader.java:418)         at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)         at java.lang.ClassLoader.loadClass(ClassLoader.java:351)         ... 1 more 

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">     <modelVersion>4.0.0</modelVersion>     <groupId>ch.jocomol.lw</groupId>     <artifactId>pdftool</artifactId>     <version>0.0.1</version>     <packaging>jar</packaging>     <dependencies>         <dependency>             <groupId>commons-logging</groupId>             <artifactId>commons-logging</artifactId>             <version>1.2</version>         </dependency>         <dependency>             <groupId>org.apache.pdfbox</groupId>             <artifactId>pdfbox</artifactId>             <version>2.0.20</version>         </dependency>         <dependency>             <groupId>org.bouncycastle</groupId>             <artifactId>bcprov-jdk15on</artifactId>             <version>1.66</version>         </dependency>     </dependencies>     <build>         <plugins>             <plugin>                 <groupId>org.apache.maven.plugins</groupId>                 <artifactId>maven-jar-plugin</artifactId>                 <version>3.1.0</version>                 <configuration>                     <finalName>pdftool</finalName>                     <archive>                         <manifest>                             <addClasspath>true</addClasspath>                             <classpathPrefix>lib/</classpathPrefix>                             <mainClass>ch.jocomol.lw.Main</mainClass>                         </manifest>                     </archive>                 </configuration>             </plugin>         </plugins>     </build>     <properties>         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>     </properties> </project> 
import org.apache.pdfbox.multipdf.PDFMergerUtility;  import java.io.File; import java.io.IOException; import java.util.Arrays; import java.util.List;  public class Main {      public static void main(String[] args) throws IOException {         List<String> argsList = Arrays.asList(args);         PDFMergerUtility PDFmerger = new PDFMergerUtility();         PDFmerger.setDestinationFileName("Unsigned_Joined_Document.pdf");         for (String pdfFile : argsList){             PDFmerger.addSource(new File(pdfFile));             System.out.println("Merging: " + pdfFile);         }         PDFmerger.mergeDocuments(null);     } } 

In other questions it was mentioned that the hard dependencies of PDFBox could be missinng but as you can see, I included the hard dependencies in my pom.xml but it didn’t change anything.

Add Comment
1 Answer(s)

Thanks to Lalit Mehra I found out what the problem was. The my Jar didn’t include the jar of PDFBox so the class wasn’t available. I solved by adding the maven-assembly-plugin to my pom.xml

            <plugin>                 <groupId>org.apache.maven.plugins</groupId>                 <!--suppress MavenModelInspection -->                 <artifactId>maven-assembly-plugin</artifactId>                 <executions>                     <execution>                         <phase>package</phase>                         <goals>                             <goal>single</goal>                         </goals>                         <configuration>                             <descriptorRefs>                                 <descriptorRef>jar-with-dependencies</descriptorRef>                             </descriptorRefs>                             <finalName>pdftool</finalName>                             <appendAssemblyId>false</appendAssemblyId>                             <archive>                                 <manifest>                                     <mainClass>                                         ch.admin.wbf.isceco.lw.Main                                     </mainClass>                                 </manifest>                             </archive>                         </configuration>                     </execution>                 </executions>             </plugin> 
Answered on July 16, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.