简体   繁体   中英

Attaching external JARs to a java project in eclipse - java.lang.NoClassDefFoundError

Trying to implement pdfbox in eclipse but I'm getting this error when I run it.

>Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory

>   at org.apache.pdfbox.pdfparser.BaseParser.<clinit>(BaseParser.java:68)

>   at com.pdf.util.PDFTextParser.<init>(PDFTextParser.java:26)

>   at com.pdf.util.PDFTextParser.main(PDFTextParser.java:77)

>Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory

>   at java.net.URLClassLoader.findClass(Unknown Source)

>   at java.lang.ClassLoader.loadClass(Unknown Source)

>   at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)

>   at java.lang.ClassLoader.loadClass(Unknown Source)

>   ... 3 more

The program stops at this line of code:

parser = new PDFParser (new FileInputStream(file));

PDFParser comes from pdfbox.

I'm guessing there's something wrong with how I've attached the JAR files?

  • I moved all the jar files to a folder I created called "lib" which is part of the project.
  • Went into project Properties -> Java Build Path, and clicked "Add External JARs" for every JAR file
  • After doing this I noticed that it said "Source attachment: none" for each of the JARs, so I clicked edit and set the destination to its location in the lib folder.
  • When I go into Run Configuration, under Classpath, I can see the JAR files are there underneath my project.

PDFBox requires Commons Logging (see this dependencies page from the project's website). You need to reference that Jar in the classpath along with the PDFBox Jar. If you use a build tool like Maven, it should automatically download it for your project.

检查链接如何在堆栈溢出中将外部 jar 添加到类路径

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM