23

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.

Cobaia
  • 1,503
  • 3
  • 22
  • 41

8 Answers8

31

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>
Daniel Rodríguez
  • 548
  • 1
  • 10
  • 30
jehon
  • 1,404
  • 15
  • 21
  • bcprov-jdk15on: 1.48 works also, and is currently included in the extra-jars ZIP file which Kıvanç Sahici mentioned above – Michael M Jul 03 '14 at 22:48
20

BouncyCastle libs are undergoing heavy API changes that broke the compatibility with other libs like iText.

Either

  • use a previous version of BouncyCastle libs. Old versions can be found here. However, you'll have to find the right version of iText that was compatible with this particular version of BC.

  • make your own build of iText (the SVN trunk has been fixed). iText can be build with Maven (there's a short readme file at the root of the SVN). Please note that it's at your own risk, there may be bugs in trunk.

  • wait for the next version of iText. From my experience, iText releases come every couple of months, sometime more often, sometimes less. I'm not an iText committer though, so I can't give you any ETA.

More information can be found in this thread

Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
  • What is your suggestion? I tried to get the SVN https://itext.svn.sourceforge.net/svnroot/itext but I do not know how to compile. I tried using an old version of BouncyCastle http://repo1.maven.org/maven2/org/bouncycastle/ without success. – Cobaia Apr 30 '12 at 23:22
  • If you are not pressed by time, I would wait for the next iText release. If you can't, I've edited my answer with more details... – Alexis Pigeon May 01 '12 at 13:03
  • 6
    For iText2.1.7 and Sun JDK 1.7 This link fixed the DEREncodable not_found issue. http://repo2.maven.org/maven2/org/bouncycastle/bcprov-jdk15%2b/1.46/bcprov-jdk15%2b-1.46.jar – so_mv Jan 19 '13 at 20:08
  • For me https://mvnrepository.com/artifact/bouncycastle/bcprov-jdk16/140 solved the problem for Java8 – Gerrit Griebel Jun 19 '19 at 12:54
14

With itextpdf version 5.5.4 org.bouncycastle dependencies are marked as <optional>true</optional>. This means you MUST include those dependencies in your own pom, or you can run into classnotfound exceptions.

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.4</version>
</dependency>

<!-- Bouncycastle dependencies necessary as they are optional = true
    in itextpdf ... but they're not-so-optional in reality -->
<dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcprov-jdk15on</artifactId>
    <version>1.49</version>
</dependency>
<dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcpkix-jdk15on</artifactId>
    <version>1.49</version>
</dependency>
CorayThan
  • 17,174
  • 28
  • 113
  • 161
5

from version of bcprov-jdk15on-147, class of DEREncodable is no longer exist under the path of org.bouncycastle.asn1. You can use version before 146 (including 146) to resolve this question.

Bill Watson
  • 51
  • 1
  • 1
  • Yes, a couple of important classes changed in version 1.47 of bouncycastle: http://www.bouncycastle.org/wiki/display/JA1/Porting+from+earlier+BC+releases+to+1.47+and+later – andrew lorien Apr 30 '15 at 23:32
1

It's strange that the jars available at bouncycastle.org don't seem to contain this class. Perhaps, you may want to use one from the locations listed in this page (link).

srkavin
  • 1,152
  • 7
  • 17
  • 2
    It blows my mind that they removed this class but still refer to it from their own jar file. We have a test which *only* touches BouncyCastle, and get the exact same error. – Hakanai Jan 17 '13 at 03:52
1

i have the same problem, but a fix it when i download the libreria and update those files on /WEBINF/LIB

Jules
  • 11
  • 1
1

Luckily, the dependency jars are being delivered along with the iText now.

Please check the repository link below and download extrajars.zip file

http://sourceforge.net/projects/itext/files/

Kıvanç Sahici
  • 300
  • 2
  • 8
0

For jruby-ers with the same failure, I updated to jruby-complete-9.1.13.0.jar from jruby-complete-1.6.6.jar and the problem seemed to resolve...FWIW...

rogerdpack
  • 62,887
  • 36
  • 269
  • 388