问题
I am using pdfbox-0.7.3.jar. I know missing related class files belongs to JAR pdfbox-0.7.3 but when i attach the source file. keep showing missing .class files. i am seeking for suggestions on the below error.
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.pdfbox.cos.COSDocument;
import org.pdfbox.pdfparser.PDFParser;
import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.util.PDFTextStripper;
import java.lang.NoClassDefFoundError;
import java.util.Scanner;
public class ggg{
public static void main(String args[]) {
// PDFTextStripper pdfStripper = null;
// PDDocument pdDoc = null;
// COSDocument cosDoc = null;
File file = new File("C:\\Users\\firstfile.pdf");
try {
PDFParser parser = new PDFParser(new FileInputStream(file));
parser.parse();
COSDocument cosDoc = parser.getDocument();
PDFTextStripper pdfStripper = new PDFTextStripper();
PDDocument pdDoc = new PDDocument(cosDoc);
pdfStripper.setStartPage(1);
pdfStripper.setEndPage(5);
String parsedText = pdfStripper.getText(pdDoc);
System.out.println(parsedText);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Exception in thread "main" java.lang.NoClassDefFoundError: org/fontbox/afm/FontMetric
at org.pdfbox.pdmodel.font.PDFont.getAFM(PDFont.java:334)
at org.pdfbox.pdmodel.font.PDSimpleFont.getFontHeight(PDSimpleFont.java:104)
at org.pdfbox.util.PDFStreamEngine.showString(PDFStreamEngine.java:336)
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 ggg.main(ggg.java:30)
回答1:
Seems that you are not using any build tool.
Unfortunately, this library has additional dependencies.
org.fontbox.afm.FontMetric is a class that is located in fontbox-0.1.0.jar
You can go to Maven Central - PDF Box and download and add all libraries mentioned in dependencies to your project.
What else you can do is to setup a maven project. And add this dependency to your pom.xml. To do this you need:
- Install maven
Create a project using maven command line command
mvn -B archetype:generate \ -DarchetypeGroupId=org.apache.maven.archetypes \ -DgroupId=com.mycompany.app \ -DartifactId=my-app
Add maven PDF dependency to pom.xml file to the section
<dependendencies>
<dependency> <groupId>pdfbox</groupId> <artifactId>pdfbox</artifactId> <version>0.7.3</version> </dependency>
Open your generated project as a Maven project inside your IDE (in your case it is Eclipse)
Refresh project in IDE and let Eclipse download library with all dependencies for you.
来源:https://stackoverflow.com/questions/44541845/java-lang-noclassdeffounderror-org-fontbox-afm-fontmetric