Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iText and org.bouncycastle.asn1.ASN1Primitive not found

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>
like image 775
phrfpeixoto Avatar asked Dec 26 '12 15:12

phrfpeixoto


2 Answers

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.

like image 120
mkl Avatar answered Sep 21 '22 15:09

mkl


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>
like image 41
MrDrews Avatar answered Sep 18 '22 15:09

MrDrews