2

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.
Sundararaj Govindasamy
  • 8,180
  • 5
  • 44
  • 77
Sentinel
  • 441
  • 1
  • 6
  • 25
  • check in your build path there is apache-common-logging.jar or stuff like this – subash Sep 24 '15 at 19:36
  • The `/lib` folder only works for `WAR`s / webapps (or similar, surely not with plain jars), because that's what the stardard states; as @subash points out, not only you need your external libraries jars, but also the jars those depend on (a.k.a. dependencies of your dependencies) – watery Sep 24 '15 at 19:39

2 Answers2

2

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.

M A
  • 71,713
  • 13
  • 134
  • 174
  • 1
    Yep, this is what I was missing. Once i added the apache commons logging jar it was up and running. – Sentinel Sep 24 '15 at 19:53
0

Check link how to add external jar to classpath in stack overflow

Nirav Patel
  • 1,304
  • 2
  • 13
  • 30