简体   繁体   English

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

[英]java.lang.ClassNotFoundException: org.apache.pdfbox.multipdf.PDFMergerUtility

I try to combine mutliple PDFs into one using PDFBox.我尝试使用 PDFBox 将多个 PDF 合并为一个。 I was following this tutorial from tutorialpoint.我是从教程点学习教程的。 But whenever I run it ( java -jar pdftool.jar./pdf-files/* ) it throws the following error.但是每当我运行它( java -jar pdftool.jar./pdf-files/* )它都会引发以下错误。 Following is the main.java file and the pom.xml for maven.以下是 maven 的 main.java 文件和 pom.xml。

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 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.在其他问题中,有人提到 PDFBox 的硬依赖关系可能会丢失,但如您所见,我在 pom.xml 中包含了硬依赖关系,但它没有改变任何东西。

Thanks to Lalit Mehra I found out what the problem was.感谢 Lalit Mehra,我发现了问题所在。 The my Jar didn't include the jar of PDFBox so the class wasn't available.我的 Jar 不包括 PDFBox 的 jar,因此 class 不可用。 I solved by adding the maven-assembly-plugin to my pom.xml我通过将 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>

暂无
暂无

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

相关问题 java.lang.ClassNotFoundException:org.apache.pdfbox.exceptions.COSVisitorException - java.lang.ClassNotFoundException: org.apache.pdfbox.exceptions.COSVisitorException org.apache.commons.lang.SerializationException:java.lang.ClassNotFoundException - org.apache.commons.lang.SerializationException: java.lang.ClassNotFoundException java.lang.ClassNotFoundException:org.apache.catalina.connector.Response - java.lang.ClassNotFoundException: org.apache.catalina.connector.Response java.lang.ClassNotFoundException:org.apache.commons.dbcp.BasicDataSource - java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource java.lang.ClassNotFoundException:org.apache.derby.jdbc.EmbeddedDriver - java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver java.lang.ClassNotFoundException:org.apache.commons.collections4.ListValuedMap - java.lang.ClassNotFoundException: org.apache.commons.collections4.ListValuedMap java.lang.ClassNotFoundException: org.apache.log4j.Logger - java.lang.ClassNotFoundException: org.apache.log4j.Logger java.lang.ClassNotFoundException:HttpPost 上的 org.apache.http.HttpEntity - java.lang.ClassNotFoundException: org.apache.http.HttpEntity on HttpPost org.apache.jasper.JasperException: java.lang.ClassNotFoundException - org.apache.jasper.JasperException: java.lang.ClassNotFoundException java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlObject 错误 - java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlObject Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM