遇到 java.lang.NoClassDefFoundError 错误与 PDFBox 相关。

huangapple go评论90阅读模式
英文:

Getting java.lang.NoClassDefFoundError with PDFBox

问题

我正在尝试从Eclipse中的Java应用程序使用PDFBox库读取PDF文件。这是一个简单的PDF文本,但我遇到了以下异常:java.lang.NoClassDefFoundError: org/apache/fontbox/FontBoxFont。

我阅读了类似的帖子,但是解决方案对我不起作用。以下是我的代码:

package pdfreader;

import java.io.*;
import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.text.*;

public class PdfReader {
    private static void readPDF(String srcPDF) {
        try {
            PDDocument document = PDDocument.load(new File(srcPDF));
            if (!document.isEncrypted()) {
                PDFTextStripper stripper = new PDFTextStripper();
                String text;

                text = stripper.getText(document);

                System.out.println("Text:" + text);
            }
            document.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String srcPDF = "Test.pdf";

        readPDF(srcPDF);
    }
}

跟踪信息如下:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/fontbox/FontBoxFont
    at org.apache.pdfbox@2.0.21/org.apache.pdfbox.pdmodel.font.PDFontFactory.createFont(PDFontFactory.java:89)
    at org.apache.pdfbox@2.0.21/org.apache.pdfbox.pdmodel.PDResources.getFont(PDResources.java:146)
    at org.apache.pdfbox@2.0.21/org.apache.pdfbox.contentstream.operator.text.SetFontAndSize.process(SetFontAndSize.java:66)
    at org.apache.pdfbox@2.0.21/org.apache.pdfbox.contentstream.PDFStreamEngine.processOperator(PDFStreamEngine.java:933)
    at org.apache.pdfbox@2.0.21/org.apache.pdfbox.contentstream.PDFStreamEngine.processStreamOperators(PDFStreamEngine.java:515)
    at org.apache.pdfbox@2.0.21/org.apache.pdfbox.contentstream.PDFStreamEngine.processStream(PDFStreamEngine.java:489)
    at org.apache.pdfbox@2.0.21/org.apache.pdfbox.contentstream.PDFStreamEngine.processPage(PDFStreamEngine.java:156)
    at org.apache.pdfbox@2.0.21/org.apache.pdfbox.text.LegacyPDFStreamEngine.processPage(LegacyPDFStreamEngine.java:144)
    at org.apache.pdfbox@2.0.21/org.apache.pdfbox.text.PDFTextStripper.processPage(PDFTextStripper.java:397)
    at org.apache.pdfbox@2.0.21/org.apache.pdfbox.text.PDFTextStripper.processPages(PDFTextStripper.java:325)
    at org.apache.pdfbox@2.0.21/org.apache.pdfbox.text.PDFTextStripper.writeText(PDFTextStripper.java:272)
    at org.apache.pdfbox@2.0.21/org.apache.pdfbox.text.PDFTextStripper.getText(PDFTextStripper.java:233)
    at PDFReader/pdfreader.PdfReader.readPDF(PdfReader.java:17)
    at PDFReader/pdfreader.PdfReader.main(PdfReader.java:32)
Caused by: java.lang.ClassNotFoundException: org.apache.fontbox.FontBoxFont
    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)
    ... 14 more

我在Eclipse的Java构建路径中包含了commons-logging-1.1.jar和pdfbox-2.0.21.jar
英文:

I'm trying to read a PDF file with PDFBox lib from a java application in Eclipse. It's a simple PDF text but I'm getting this exception java.lang.NoClassDefFoundError: org/apache/fontbox/FontBoxFont.

I read similar posts but the solutions aren't working for me. Here is my code:

package pdfreader;
import java.io.*;
import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.text.*;
public class PdfReader {
private static void readPDF(String srcPDF) {
try {
PDDocument document = PDDocument.load(new File(srcPDF));
if (!document.isEncrypted()) {
PDFTextStripper stripper = new PDFTextStripper();
String text;
text = stripper.getText(document);
System.out.println("Text:" + text);
}
document.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
String srcPDF = "Test.pdf";
readPDF(srcPDF);
}
}

The trace is:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/fontbox/FontBoxFont
at org.apache.pdfbox@2.0.21/org.apache.pdfbox.pdmodel.font.PDFontFactory.createFont(PDFontFactory.java:89)
at org.apache.pdfbox@2.0.21/org.apache.pdfbox.pdmodel.PDResources.getFont(PDResources.java:146)
at org.apache.pdfbox@2.0.21/org.apache.pdfbox.contentstream.operator.text.SetFontAndSize.process(SetFontAndSize.java:66)
at org.apache.pdfbox@2.0.21/org.apache.pdfbox.contentstream.PDFStreamEngine.processOperator(PDFStreamEngine.java:933)
at org.apache.pdfbox@2.0.21/org.apache.pdfbox.contentstream.PDFStreamEngine.processStreamOperators(PDFStreamEngine.java:515)
at org.apache.pdfbox@2.0.21/org.apache.pdfbox.contentstream.PDFStreamEngine.processStream(PDFStreamEngine.java:489)
at org.apache.pdfbox@2.0.21/org.apache.pdfbox.contentstream.PDFStreamEngine.processPage(PDFStreamEngine.java:156)
at org.apache.pdfbox@2.0.21/org.apache.pdfbox.text.LegacyPDFStreamEngine.processPage(LegacyPDFStreamEngine.java:144)
at org.apache.pdfbox@2.0.21/org.apache.pdfbox.text.PDFTextStripper.processPage(PDFTextStripper.java:397)
at org.apache.pdfbox@2.0.21/org.apache.pdfbox.text.PDFTextStripper.processPages(PDFTextStripper.java:325)
at org.apache.pdfbox@2.0.21/org.apache.pdfbox.text.PDFTextStripper.writeText(PDFTextStripper.java:272)
at org.apache.pdfbox@2.0.21/org.apache.pdfbox.text.PDFTextStripper.getText(PDFTextStripper.java:233)
at PDFReader/pdfreader.PdfReader.readPDF(PdfReader.java:17)
at PDFReader/pdfreader.PdfReader.main(PdfReader.java:32)
Caused by: java.lang.ClassNotFoundException: org.apache.fontbox.FontBoxFont
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)
... 14 more

I included commons-logging-1.1.jar and pdfbox-2.0.21.jar on my Java Build Path in Eclipse.

答案1

得分: 0

我解决方法是将 fontbox-2.0.21.jar 添加到我的路径中。

英文:

I solve it adding fontbox-2.0.21.jar on my path.

huangapple
  • 本文由 发表于 2020年8月28日 01:07:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63620978.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定