Search code examples
javaitextbouncycastlepdf-conversionghost4j

Getting NoClassDefFoundError by using Ghost4j


this is my first Questing here and i didn't found any solutions to my Problem. Please don't take it amiss, if my text is broken English.

For my program I want to resize images inside an existing PDF-Document. This should happens automatically within a Java program. During my search, I found the Ghost4j Library in the web, which can solve my problem - maybe!

As a first test with Ghost4j to tryout if it works, I want to load my PDF Document out of the MySQL Database and check out the pageCount.

Here ist my short code:

... 
for (File file : convertableFiles) {
        InputStream inputStream = new ByteArrayInputStream(file.getFile());

        PDFDocument doc = new PDFDocument();
        doc.load(inputStream);
        System.out.println(doc.getPageCount());
}
...

The error comes at line 45 = doc.load(inputStream)

(Note: if I use new File(Path) for doc.load and set a pdfSample Document. It works. But not with inputStream)

When I execute my programm i'll get everytime this Excption:

Sep 29, 2014 4:54:53 PM ch.carauktion.dbresize.DBFileResizer convert
INFORMATION: P1 (asc): 0 / 1
Sep 29, 2014 4:54:54 PM ch.carauktion.dbresize.DBFileResizer run
SCHWERWIEGEND: P1 (asc): Exception
java.lang.NoClassDefFoundError: org/bouncycastle/asn1/ASN1OctetString
    at com.lowagie.text.pdf.PdfEncryption.<init>(Unknown Source)
    at com.lowagie.text.pdf.PdfReader.readDecryptedDocObj(Unknown Source)
    at com.lowagie.text.pdf.PdfReader.readDocObj(Unknown Source)
    at com.lowagie.text.pdf.PdfReader.readPdf(Unknown Source)
    at com.lowagie.text.pdf.PdfReader.<init>(Unknown Source)
    at com.lowagie.text.pdf.PdfReader.<init>(Unknown Source)
    at org.ghost4j.document.PDFDocument.load(PDFDocument.java:45)
    at ch.carauktion.dbresize.pdf.DBPdfResizer.convertFiles(DBPdfResizer.java:50)
    at ch.carauktion.dbresize.DBFileResizer.convert(DBFileResizer.java:114)
    at ch.carauktion.dbresize.DBFileResizer.run(DBFileResizer.java:59)
Caused by: java.lang.ClassNotFoundException: org.bouncycastle.asn1.ASN1OctetString
    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:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 10 more

For this Project implements Libraries, which all comes from the downloaded ghost4j package:

  • ghost4j-0.5.1
  • iText-2.1.7
  • jna-3.3.0
  • log4j-1.2.15
  • commons-logging-1.1.1
  • commons-io-1.3.1
  • commons-beanutils-1.8.3

Example Sites at my search for this Error:

http://sourceforge.net/p/itext/mailman/itext-questions/thread/[email protected]/

http://itext-general.2136553.n4.nabble.com/java-lang-NoClassDefFoundError-org-bouncycastle-asn1-ASN1OctetString-td3427288.html

I understand that iText 2.1.7 isn't supported anymore and i should use Version 5.x.x, but it doesn't work here to download the newest iText Lib, when in the Ghost4j Jar apparently the Lib 2.1.7 are used. Otherwise, maybe its my fault and I didn't understand at this moment how to implements correctly the newest Version.

PS: I'am using Java 1.7, Eclipse Kepler, Windows 8.1

I'll be glad, someone is knowing any solutions or can help me a little bit.

Wudmaan


Solution

  • You are missing the Bouncycastle dependency.

    I don't think a PDF library would depends on that, except if there is a need to protect the PDF, but you'll find Bouncycastle here: http://bouncycastle.org/latest_releases.html

    Try with bcprov-jdk14-147.jar and/or bcprov-ext-jdk14-147.jar downloadable from Maven Central repository:

    If that still don't work, try with the other excluded dependencies listed here:

    <dependency>
        <groupId>com.lowagie</groupId>
        <artifactId>itext</artifactId>
        <version>2.1.7</version>
        <exclusions>
            <exclusion>
                <artifactId>bcmail-jdk14</artifactId>
                <groupId>bouncycastle</groupId>
            </exclusion>
            <exclusion>
                <artifactId>bcmail-jdk14</artifactId>
                <groupId>org.bouncycastle</groupId>
            </exclusion>
            <exclusion>
                <artifactId>bcprov-jdk14</artifactId>
                <groupId>bouncycastle</groupId>
            </exclusion>
            <exclusion>
                <artifactId>bcprov-jdk14</artifactId>
                <groupId>org.bouncycastle</groupId>
            </exclusion>
            <exclusion>
                <artifactId>bctsp-jdk14</artifactId>
                <groupId>org.bouncycastle</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    

    Note: you should use Maven to take of those dependencies.