iText / BouncyCastle throws "java.lang.VerifyError: class overrides final method equals"

17,149

Solution 1

My best guess is that you have ended up with two different versions of Bouncy Castle on your classpath, and it happened so that the classloader has loaded the superclass from one version and is now trying to load the subclass from the other. The versions are different in that one of them defines a final equals method.

Solution 2

Had same error, my solution might come in handy. In my case all i was doing was digital signature of pdf documents, using Maven i had both IText(itextpdf.jar/version 5.4.2) & Bouncycastle(bcprov-jdk15on.jar/version 1.55) dependency in my pom.xml. Then i read a part in this iText book Digital Signatures for PDF documents about Bouncycastle-related problems.I removed the Bouncycastle(bcprov-jdk15on.jar/version 1.55) dependency and the error was gone(noob mistake- the bouncycastle dependency was already part of the itext dependency no need to have the two seperately).

NOTE: if you run into any Bouncycastle class file not found errors after removing bouncycastle dependency check this

Solution 3

Had the same problem, and it got solved in a wierd way. All i had to do is to add bcprov-ext

    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcprov-ext-jdk15on</artifactId>
        <version>1.52</version>
    </dependency>

Solution 4

I had similiar problem because I had two different artifacts on my classpath:

<groupId>org.bouncycastle</groupId>
<artifactId>bcprov</artifactId>

and

 <groupId>org.bouncycastle</groupId>
 <artifactId>bcprov-jdk15on</artifactId>

I have excluded the bcprov, which solved the issue for me.

Share:
17,149
m00hk00h
Author by

m00hk00h

Updated on June 22, 2022

Comments

  • m00hk00h
    m00hk00h about 2 years

    We have an application that creates PDFs unsing jasperreports.
    It also manipulates said PDFs using iText after they have been created.

    We recently started using encryption on some PDF. That means before the app can handle the PDF after its creation, it has to be decrypted. While attempting to do so using iText's PdfReader(String path, byte[] password) I get the following exception:

    java.lang.VerifyError: class org.bouncycastle.asn1.ASN1Primitive overrides final method equals.(Ljava/lang/Object;)Z
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClassCond(Unknown Source)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at com.simontuffs.onejar.JarClassLoader.defineClass(JarClassLoader.java:561)
        at com.simontuffs.onejar.JarClassLoader.findClass(JarClassLoader.java:475)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at com.itextpdf.text.pdf.PdfEncryption.<init>(PdfEncryption.java:148)
        at com.itextpdf.text.pdf.PdfReader.readDecryptedDocObj(PdfReader.java:914)
        at com.itextpdf.text.pdf.PdfReader.readDocObj(PdfReader.java:1294)
        at com.itextpdf.text.pdf.PdfReader.readPdf(PdfReader.java:643)
        at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:187)
        at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:212)
        at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:202)
    

    The project is built as a runnable .jar using Maven and uses the following dependencies:
    iText 5.4.2
    bouncycastle 1.48

    I should mention that jasperreports has its own dependency of iText and bouncycastle:
    iText 2.1.7
    bouncycastle 1.38

    I can't figure out what's going on on and need help.

  • mkl
    mkl about 11 years
    +1: ASN1Primitive is derived from ASN1Object. Up to BC 1.46, ASN1Object.equals used to be final but since 1.47 it is not final anymore. ASN1Primitive is only present in BC since version 1.47 and overrides equals. Most likely, therefore, ASN1Object in the OP's is loaded from BC 1.38 and ASN1Primitive from BC 1.48.
  • m00hk00h
    m00hk00h about 11 years
    Thanks a lot, Marko. mkl, too. I will try to resolve this issue. Will accept your answer if it really was the cause.
  • m00hk00h
    m00hk00h about 11 years
    Thanks again, that really was the source of the problem. I ended up writing a small encrypter/decrypter-adapter-class. It accepts the encrypted PDFs generated by jasperreports, decrypts them using the bouncycastle-dependency of Jasper/iText 2.1.7 and sends the decrypted PDF to the classes that do all the other shenanigans using iText 5.4.2. This way there is no need for the bouncycastle dependency of iText 5.4.2 which I simply removed now. Works flawlessly.
  • tbraun
    tbraun over 10 years
    Thanks! In my case, I was using jruby and my jruby bouncycastle version was different from my project library. Both are now running 1.46 and it's working fine! Java Maven:org.bouncycastle,bcprov-jdk15on,1.46; Ruby gems:bouncy-castle-java (1.5.0146.1);jruby-openssl (0.8.2)
  • joensson
    joensson about 10 years
    @mserioli On my system I had both the bcprov-jdk14 and the bcprov-jdk15 in revision 1.49. To fix the verifyerror I added the bcprov-ext-jdk14 (which contains the ASN classes in the jdk14 version). I guess another way of fixing it would be to make sure only one of the bcprov version were included.
  • demian
    demian about 5 years
    it worked for me. thanks. In my case I had diferent versions in my classpath and in [JAVA_HOME]/jre/lib/ext/