1
votes

I have a method that creates a PDF and saves it in the user's Documents folder (Windows, of course) with a file name of "temp.pdf". The last line in this method calls another method tht is intended to print the pdf file to paper. Unfortunately, I'm getting the following error:

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory

I'm using PDFBox to do the printing. The code isn't too involved, but I can't figure out what I'm doing wrong. Everything compiles just fine. This is a run time error. Can anyone see my mistake? Thanks.

public static void printToPaper(String fPath) throws Exception {
        System.out.println("1");  //outputs correctly
        PDDocument document = new PDDocument();   //error occurs right here
        System.out.println("2");  //never appears in output queue
        document = PDDocument.load(fPath);  
        PrinterJob printJob = PrinterJob.getPrinterJob();
        PrintService[] printService = PrinterJob.lookupPrintServices();
        printJob.setPrintService(printService[0]);
        document.silentPrint( printJob );
        document.close();
    }

Here is the full error log:

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory at org.apache.pdfbox.cos.COSDocument.(COSDocument.java:49) at org.apache.pdfbox.pdmodel.PDDocument.(PDDocument.java:125) at kidmath.PrintPDF.printToPaper(PrintPDF.java:12) at kidmath.Print.printPaper(Print.java:83) at kidmath.JDlgPrintSettings.btnPrintMouseClicked(JDlgPrintSettings.java:271) at kidmath.JDlgPrintSettings.access$600(JDlgPrintSettings.java:10) at kidmath.JDlgPrintSettings$7.mouseClicked(JDlgPrintSettings.java:170) at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270) at java.awt.Component.processMouseEvent(Component.java:6508) at javax.swing.JComponent.processMouseEvent(JComponent.java:3321) at java.awt.Component.processEvent(Component.java:6270) at java.awt.Container.processEvent(Container.java:2229) at java.awt.Component.dispatchEventImpl(Component.java:4861) at java.awt.Container.dispatchEventImpl(Container.java:2287) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4501) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) at java.awt.Container.dispatchEventImpl(Container.java:2273) at java.awt.Window.dispatchEventImpl(Window.java:2719) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:703) at java.awt.EventQueue.access$000(EventQueue.java:102) at java.awt.EventQueue$3.run(EventQueue.java:662) at java.awt.EventQueue$3.run(EventQueue.java:660) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) at java.awt.EventQueue$4.run(EventQueue.java:676) at java.awt.EventQueue$4.run(EventQueue.java:674) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:673) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139) at java.awt.EventDispatchThread.run(EventDispatchThread.java:97) Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory at java.net.URLClassLoader$1.run(URLClassLoader.java:366) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:423) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:356) ... 39 more

2
Could you post the full log. There must be a ClassNotFound exception down somewhere. Include all related jars, it should work. - Jayan
Done. See above. Thanks! - AndroidDev

2 Answers

3
votes

Include commons logging to your class path and that should fix it. download link is here http://commons.apache.org/logging/

and the class that you are referring to is http://commons.apache.org/logging/commons-logging-1.0.3/docs/api/org/apache/commons/logging/LogFactory.html May be it's internally being referred from PDFBox API.

1
votes

Try initializing the PDDocument class first before loading the file path.

PDDocument document = new PDDocument();
document = PDDocument.load(fPath);

That should fix it.