java.lang.ClassNotFoundException: org.apache.pdfbox.multipdf.PDFMergerUtility
java.lang.ClassNotFoundException: org.apache.pdfbox.multipdf.PDFMergerUtility
我尝试使用 PDFBox 将多个 PDF 合并为一个。我正在关注 tutorialpoint 的 this 教程。但是每当我 运行 它 (java -jar pdftool.jar ./pdf-files/*
) 它抛出以下错误。以下是 Maven 的 main.java 文件和 pom.xml。
错误
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);
}
}
在其他问题中提到 PDFBox 的硬依赖性可能会丢失,但如您所见,我在我的 pom.xml 中包含了硬依赖性,但它没有改变任何东西。
感谢 Lalit Mehra,我发现了问题所在。
我的 Jar 不包含 PDFBox 的 jar,因此 class 不可用。
我通过将 maven-assembly-plugin 添加到我的 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>
我尝试使用 PDFBox 将多个 PDF 合并为一个。我正在关注 tutorialpoint 的 this 教程。但是每当我 运行 它 (java -jar pdftool.jar ./pdf-files/*
) 它抛出以下错误。以下是 Maven 的 main.java 文件和 pom.xml。
错误
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);
}
}
在其他问题中提到 PDFBox 的硬依赖性可能会丢失,但如您所见,我在我的 pom.xml 中包含了硬依赖性,但它没有改变任何东西。
感谢 Lalit Mehra,我发现了问题所在。 我的 Jar 不包含 PDFBox 的 jar,因此 class 不可用。 我通过将 maven-assembly-plugin 添加到我的 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>