7

I'm a newbie on iText. This is my first project using this library.

I'm building a PDF with essentially a big table on it, and while compiling, i'm getting this Class Not Found error: class file for org.bouncycastle.asn1.ASN1Primitive not found

I'm confused, since i'm only using the basic functionalities, and didn't even touch the PDF Signing features. What should i do to fix the error?

I'm using:

  • JDK 1.7
  • iText 5.3.5
  • extrajars 2.2 (which provides bcmail-jdk15-146.jar, bcprov-jdk15-146.jar and bctsp-jdk15-146.jar)

I only using iText inside one class, with these imports:

import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

If it helps, i would like to clarify that when i run the project inside NetBeans, it compiles and runs just fine. The error appears when i try to compile it to a single executable jar file (which includes the dist/lib)

This is build.xml target where the error appears:

<target name="single_jar" depends="jar">

    <property name="store.jar.name" value="Final"/>

    <property name="store.dir" value="store"/>
    <property name="store.jar" value="${store.dir}/${store.jar.name}.jar"/>

    <echo message="Packaging ${application.title} into a single JAR at ${store.jar}"/>

    <delete dir="${store.dir}"/>
    <mkdir dir="${store.dir}"/>

    <jar destfile="${store.dir}/temp_final.jar" filesetmanifest="skip">
        <zipgroupfileset dir="dist" includes="*.jar"/>
        <zipgroupfileset dir="dist/lib" includes="*.jar"/>

        <manifest>
            <attribute name="Main-Class" value="${main.class}"/>
        </manifest>
    </jar>

    <zip destfile="${store.jar}">
        <zipfileset src="${store.dir}/temp_final.jar"
        excludes="META-INF/*.SF, META-INF/*.DSA, META-INF/*.RSA"/>
    </zip>

    <delete file="${store.dir}/temp_final.jar"/>

</target>
phrfpeixoto
  • 191
  • 1
  • 2
  • 11
  • Current iText versions (e.g. 5.3.5) use BouncyCastle 1.47 but you provide 1.46. Please update dependencies. – mkl Dec 26 '12 at 15:45
  • I used the jars provided on the latest extrajars package on the iText project. I'll try 1.47 and post back the results. – phrfpeixoto Dec 26 '12 at 15:50
  • Hi, it worked as you stated. Thank you so much. Please post the answer, so i can mark it as correct. – phrfpeixoto Dec 26 '12 at 16:00

2 Answers2

12

Current iText versions (since 5.3.0) use BouncyCastle 1.47 but you provide 1.46; even though that looks like a small step, there are substantial changes between those BC versions; any sensible version management would have called it 2.0.

Please update dependencies.

mkl
  • 90,588
  • 15
  • 125
  • 265
  • I'd like to note (for others with the same problem) that the extrajars-2.2 package, provided by iText 5.3.5 includes BC version 1.46, which won't work. – phrfpeixoto Dec 27 '12 at 11:33
  • As a side note on dependencies... iText has been mavenized. If you require information on the dependencies, simply inspect its pom.xml. If your project is using maven, too, the correct dependencies automatically are used. – mkl Dec 28 '12 at 12:07
  • 1
    BTW, currently (i.e. as of iText 5.4.1) BC 1.48 is in use. Thus, always check the BC version required by your iText version. – mkl Apr 02 '13 at 21:41
4

I was getting java.lang.NoClassDefFoundError: org/bouncycastle/asn1/ASN1Primitive when depending on:

    <dependency>                    
        <groupId>com.itextpdf.tool</groupId>
        <artifactId>xmlworker</artifactId>
        <version>5.5.0</version>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itextpdf</artifactId>
        <version>5.5.0</version>
    </dependency>

I needed to explicitly include newer bouncycastle artifacts:

    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcprov-jdk15on</artifactId>
        <version>1.50</version>
    </dependency>
    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcpkix-jdk15on</artifactId>
        <version>1.50</version>
    </dependency>        
    <dependency>                    
        <groupId>com.itextpdf.tool</groupId>
        <artifactId>xmlworker</artifactId>
        <version>5.5.0</version>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itextpdf</artifactId>
        <version>5.5.0</version>
    </dependency>
MrDrews
  • 2,139
  • 2
  • 22
  • 22