获取 java.lang.NoClassDefFoundError: org/pdfbox/pdfparser/

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18503159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 08:37:31  来源:igfitidea点击:

Getting java.lang.NoClassDefFoundError: org/pdfbox/pdfparser/

javapdfbox

提问by Paras

Below is the code that I am using, I've provided one pdf file and one text file as an input to command line.

下面是我正在使用的代码,我提供了一个 pdf 文件和一个文本文件作为命令行的输入。

import org.pdfbox.cos.COSDocument;
import org.pdfbox.pdfparser.PDFParser;
import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.pdmodel.PDDocumentInformation;
import org.pdfbox.util.PDFTextStripper;

import java.io.File;
import java.io.FileInputStream;
import java.io.PrintWriter;

public class PDFTextParser {

    PDFParser parser;
    String parsedText;
    PDFTextStripper pdfStripper;
    PDDocument pdDoc;
    COSDocument cosDoc;
    PDDocumentInformation pdDocInfo;

    // PDFTextParser Constructor 
    public PDFTextParser() {
    }

    // Extract text from PDF Document
    String pdftoText(String fileName) {

        System.out.println("Parsing text from PDF file " + fileName + "....");
        File f = new File(fileName);

        if (!f.isFile()) {
            System.out.println("File " + fileName + " does not exist.");
            return null;
        }

        try {
            parser = new PDFParser(new FileInputStream(f));
        } catch (Exception e) {
            System.out.println("Unable to open PDF Parser.");
            return null;
        }

        try {
            parser.parse();
            cosDoc = parser.getDocument();
            pdfStripper = new PDFTextStripper();
            pdDoc = new PDDocument(cosDoc);
            parsedText = pdfStripper.getText(pdDoc);
        } catch (Exception e) {
            System.out.println("An exception occured in parsing the PDF Document.");
            e.printStackTrace();
            try {
                if (cosDoc != null) cosDoc.close();
                if (pdDoc != null) pdDoc.close();
            } catch (Exception e1) {
                e.printStackTrace();
            }
            return null;
        }
        System.out.println("Done.");
        return parsedText;
    }

    // Write the parsed text from PDF to a file
    void writeTexttoFile(String pdfText, String fileName) {

        System.out.println("\nWriting PDF text to output text file " + fileName + "....");
        try {
            PrintWriter pw = new PrintWriter(fileName);
            pw.print(pdfText);
            pw.close();  
        } catch (Exception e) {
            System.out.println("An exception occured in writing the pdf text to file.");
            e.printStackTrace();
        }
        System.out.println("Done.");
    }

    //Extracts text from a PDF Document and writes it to a text file
    public static void main(String args[]) {

        if (args.length != 2) {
            System.out.println("Usage: java PDFTextParser  ");
            System.exit(1);
        }

        PDFTextParser pdfTextParserObj = new PDFTextParser();
        String pdfToText = pdfTextParserObj.pdftoText(args[0]);

        if (pdfToText == null) {
            System.out.println("PDF to Text Conversion failed.");
        }
        else {
            System.out.println("\nThe text parsed from the PDF Document....\n" + pdfToText);
            pdfTextParserObj.writeTexttoFile(pdfToText, args[1]);
        }
    }
}

After Running this code via command line with 2 inputs one pdf file name and another text file name I am getting noClassDefFound exception. Below is the stack trace.

通过命令行运行此代码并输入 2 个 pdf 文件名和另一个文本文件名后,我收到 noClassDefFound 异常。下面是堆栈跟踪。

Exception in thread "main" java.lang.NoClassDefFoundError: org/pdfbox/pdfparser/
PDFParser
        at PDFTextParser.pdftoText(PDFTextParser.java:42)
        at PDFTextParser.main(PDFTextParser.java:93)
Caused by: java.lang.ClassNotFoundException: org.pdfbox.pdfparser.PDFParser
        at java.net.URLClassLoader.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
        ... 2 more

After adding class path I am getting below Exceptions

添加类路径后,我低于异常

Exception in thread "main" java.lang.NoClassDefFoundError: org/fontbox/afm/AFMParser
        at org.pdfbox.pdmodel.font.PDFont.getAFM(PDFont.java:350)
        at org.pdfbox.pdmodel.font.PDFont.getAverageFontWidthFromAFMFile(PDFont.java:313)
        at org.pdfbox.pdmodel.font.PDSimpleFont.getAverageFontWidth(PDSimpleFont.java:231)
        at org.pdfbox.util.PDFStreamEngine.showString(PDFStreamEngine.java:276)
        at org.pdfbox.util.operator.ShowTextGlyph.process(ShowTextGlyph.java:80)
        at org.pdfbox.util.PDFStreamEngine.processOperator(PDFStreamEngine.java:452)
        at org.pdfbox.util.PDFStreamEngine.processSubStream(PDFStreamEngine.java:215)
        at org.pdfbox.util.PDFStreamEngine.processStream(PDFStreamEngine.java:174)
        at org.pdfbox.util.PDFTextStripper.processPage(PDFTextStripper.java:336)
        at org.pdfbox.util.PDFTextStripper.processPages(PDFTextStripper.java:259)
        at org.pdfbox.util.PDFTextStripper.writeText(PDFTextStripper.java:216)
        at org.pdfbox.util.PDFTextStripper.getText(PDFTextStripper.java:149)
        at PDFTextParser.pdftoText(PDFTextParser.java:53)
        at PDFTextParser.main(PDFTextParser.java:93)
Caused by: java.lang.ClassNotFoundException: org.fontbox.afm.AFMParser
        at java.net.URLClassLoader.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
        ... 14 more

回答by Du?an Rychnovsky

When running a program with external dependencies (PDF parser in your case) in Java you have to include the appropriate JAR files to your CLASSPATH.

在 Java 中运行具有外部依赖项(在您的情况下为 PDF 解析器)的程序时,您必须将适当的 JAR 文件包含到您的 CLASSPATH 中。

See this other thread on Stack Overflowor the documentation at Oracle.

请参阅Stack Overflow 上的其他线程Oracle 上的文档

I have searched for the classes that cannot be found at jarfinder.com and it turns out you need to include (at least) two different JAR files - the font boxand the pdf boxJARs.

我搜索了在 jarfinder.com 上找不到的类,结果证明您需要(至少)包含两个不同的 JAR 文件 -字体框pdf 框JAR。

回答by Paras

I've used iTextlibrary to solve my purpose. Its working pretty well for me.

我使用iText库来解决我的目的。它对我来说工作得很好。

回答by James879

You can get the fontbox jar from here http://mvnrepository.com/artifact/org.apache.pdfbox/fontbox/1.8.5

您可以从这里获得字体盒 jar http://mvnrepository.com/artifact/org.apache.pdfbox/fontbox/1.8.5

If you're using maven org.apache.pdfbox fontbox 1.8.5

如果您使用的是 maven org.apache.pdfbox fontbox 1.8.5

回答by Marco

I resolved this issue inserting transitive = trueinside my ivy file for pdfbox dependency.

我解决了transitive = true在我的常春藤文件中插入pdfbox 依赖项的这个问题。

<dependency org="org.apache.pdfbox" name="pdfbox" rev="1.8.11" transitive="true"/>

回答by SiddP

You need to include fontbox-1.3.1jar in the class path apart from Apache pdfbox jarwhich will fix your issue as PDFBox internally uses fontbox-1.3.1

除了Apache pdfbox jar之外,您还需要在类路径中包含fontbox-1.3.1jar,这将解决您的问题,因为 PDFBox 内部使用fontbox-1.3.1