4

I have been trying to use a Jar file as a library in my code, and it compiles fine. However, at runtime, I keep getting the NoClassDefFoundError message. Why is this happening? I have included the Jar file in the compile path and the runtime path too.

Here is the error message:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory at org.apache.pdfbox.cos.COSDocument.(COSDocument.java:51) at org.apache.pdfbox.pdmodel.PDDocument.(PDDocument.java:136) at processing.PDFToJPG.main(PDFToJPG.java:58)

Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 3 more

Here is my code:

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;

public static void main(String[] args) {
    try {
        PDDocument doc = new PDDocument();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

I am using NetBeans IDE as well as Windows 10

This is my setting for compile classpath:

[This is my setting for compile classpath[1]

This is my setting for runtime classpath:

[This is my setting for runtime classpath[2]

EDIT: Thank you for your help, it really worked. All i needed to do was to download the dependencies Jar file, not editing the classpath like what i have been trying to do

Tilman Hausherr
  • 17,731
  • 7
  • 58
  • 97
Le Hoang Long
  • 428
  • 3
  • 10
  • Have you looked at such questions as ["*Why am I getting a NoClassDefFoundError in Java?*"](http://stackoverflow.com/q/34413/1364007)? – Wai Ha Lee Dec 16 '15 at 20:37
  • I looked at a lot of articles, all of them were about classpaths. Turns out there were a dependency aspect between PDFbox and common-logging that i didn't know about – Le Hoang Long Dec 17 '15 at 09:20
  • Your PDFBox version is outdated. 1.8.10 is current. (Or 2.0 RC2 if you're a really cool person) – Tilman Hausherr Dec 17 '15 at 09:58

2 Answers2

4

I think you need another jars besides the one you have already included. Try to add common-logging 1.4. Apparently, there is a dependency between pdfbox1.8.jar and this jar as stated on their site.

EDIT: There are more dependencies fontbox and jempbox to take in account as well.

EDIT2: I made a zip with all dependencies needed you can download it here.

Aurelien
  • 458
  • 1
  • 5
  • 13
1

I agree with Aurelien's post: it looks like you are missing Apache Commons Logging - and other runtime dependencies.

You might want to consider creating your project as a 'Maven' Project (And Netbeans supports Maven pretty well): and then adding 'pdfbox' as a 'dependency'; this should make life a lot easier for you - since Maven will fetch any other required dependencies.

You can get the 'Maven Coordinates' for the various PDFBox versions from here:

http://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox

If you want to build your final project into a single JAR containing all the deps; or to create a separate 'lib' directory of them: you will have to make some minor changes to the Maven project file ('pom.xml') to do this.

This Stackoverflow Post has an example of doing that.

Community
  • 1
  • 1
monojohnny
  • 5,894
  • 16
  • 59
  • 83