iText/BouncyCastle ClassNotFound org.bouncycastle.asn1.DEREncodable and org.bouncycastle.tsp.TimeStampTokenInfo

Cobaia picture Cobaia · May 1, 2012 · Viewed 74.5k times · Source

I'm trying to use iText Java. When you run the example "how to sign" the following error occurs:

Caused by: java.lang.ClassNotFoundException: org.bouncycastle.tsp.TimeStampTokenInfo

According "Getting Started with iText - How to sign a PDF using iText", I have to use the BouncyCastle.

I downloaded the file: bcprov-jdk15on-147.jar from BouncyCastle download page.
And added to the project: Java Build Path/Libraries/Add External JARs...

I added the following line:

Security.addProvider(new BouncyCastleProvider());

When you run the example the same error occurs.
So I downloaded another file: bcpkix-jdk15on-147.jar entitled "PKIX/CMS/EAC/PKCS/OCSP/TSP/OPENSSL"
And added to the project: Java Build Path/Libraries/Add External JARs...
Now I have two Jars.

When you run the example the following error occurs:

Caused by: java.lang.ClassNotFoundException: org.bouncycastle.asn1.DEREncodable

I tried downloading the file "bcprov-ext-jdk15on-147.jar" but did not solve the problem.

I am using iText 5.2.1 and eclipse on Windows 7 64 bits.

Answer

jehon picture jehon · Jul 31, 2013

iText marks bouncycastle dependencies as optional. If you require them, you need to add the dependencies in your own pom file.

To find out which dependency to include in your project, open the itextpdf pom.xml file of the version you are using (for example 5.3.2, here) and search for the 2 bouncycastle dependencies.

    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcprov-jdk15on</artifactId>
        <version>1.47</version>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcmail-jdk15on</artifactId>
        <version>1.47</version>
        <optional>true</optional>
    </dependency>

Copy them into your pom file and remove the optional option.

    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itextpdf</artifactId>
        <version>5.3.2</version>
    </dependency>
    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcprov-jdk15on</artifactId>
        <version>1.47</version>
    </dependency>
    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcmail-jdk15on</artifactId>
        <version>1.47</version>
    </dependency>