iText/BouncyCastle ClassNotFound org.bouncycastle.

2019-01-06 17:00发布

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.

8条回答
成全新的幸福
2楼-- · 2019-01-06 17:41

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>
查看更多
混吃等死
3楼-- · 2019-01-06 17:43

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.

查看更多
乱世女痞
4楼-- · 2019-01-06 17:44

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

查看更多
何必那么认真
5楼-- · 2019-01-06 17:50

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).

查看更多
beautiful°
6楼-- · 2019-01-06 17:50

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/

查看更多
乱世女痞
7楼-- · 2019-01-06 18:02

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>
查看更多
登录 后发表回答