Java.lang. ExceptionInInitializerError in pdf library

Edit: NoClassDefFoundError not the issue, scroll to bottom of this post, error currently happening is ExceptionInInitializerError

I am using aspose.pdf for java in a microservice to generate a pdf from html content. While working on this running the service locally (on my MacBook Pro 2013) I did not face any issues. Once deploying to a testing region, however, I got

{
  "message": "There was an error processing your request. It has been logged (ID 4c209bcc5805e443)."
}

from the service and in the logs I saw

java.lang.NoClassDefFoundError: Could not initialize class com.aspose.pdf.internal.p153.z2
	at com.aspose.pdf.internal.p163.z5.m1(Unknown Source)
	at com.aspose.pdf.internal.p145.z7.m1(Unknown Source)
	at com.aspose.pdf.internal.p145.z7.m4(Unknown Source)
	at com.aspose.pdf.internal.p330.z3.m1(Unknown Source)
	at com.aspose.pdf.internal.p330.z3.m1(Unknown Source)
	at com.aspose.pdf.internal.p327.z5.m1(Unknown Source)
	at com.aspose.pdf.internal.p327.z5.m1(Unknown Source)
	at com.aspose.pdf.internal.p363.z1.m1(Unknown Source)
	at com.aspose.pdf.z36.m1(Unknown Source)
	at com.aspose.pdf.ADocument.m1(Unknown Source)
	at com.aspose.pdf.ADocument.<init>(Unknown Source)
	at com.aspose.pdf.Document.<init>(Unknown Source)

This is all trigger when the following line is executed:

            Document doc = new Document(inputHtmlStream, htmlLoadOptions);

Again, this all works fine when I run the service locally, and here is the code before that fateful line:

            HtmlLoadOptions htmlLoadOptions = new HtmlLoadOptions();
            PageInfo pageInfo = new PageInfo();
            
            double height = sizeLayoutConfig.getPageHeightDouble();
            double width = sizeLayoutConfig.getPageWidthDouble();
            pageInfo.setHeight(height);
            pageInfo.setWidth(width);
            
            MarginInfo marginInfo = new MarginInfo();
            marginInfo.setTop(sizeLayoutConfig.getMarginTopDouble());
            marginInfo.setLeft(sizeLayoutConfig.getMarginLeftDouble());
            
            pageInfo.setMargin(marginInfo);
            htmlLoadOptions.setPageInfo(pageInfo);         
            
            // Provide input stream which is read from to create intermediate document object
            InputStream inputHtmlStream = new ByteArrayInputStream(content.getBytes(ENCODING));

Content is a string.

Any help here would be appreciated.

Thanks

Michael

@mkochu,
Kindly send us your source HTML document and the complete code snippet ( including sizeLayoutConfig object values ). We also required all details of the computer where this error occurred, e.g. Operating System (name & edition), JDK (version and update), IDE and some other handy information which could help us to replicate the error in our environment. We will investigate and share our findings with you. Your response is awaited.

Best Regards,
Imran Rafique

Sure. Also, I should clarify the behavior. The first time I try to convert the html to pdf, I get an OutOfMemoryError as you see here:

java.lang.OutOfMemoryError: Java heap space
	at com.aspose.pdf.internal.p1.z14.m1(Unknown Source)
	at com.aspose.pdf.internal.p1.z14.m1(Unknown Source)
	at com.aspose.pdf.internal.p153.z2.<clinit>(Unknown Source)
	at com.aspose.pdf.internal.p163.z5.m1(Unknown Source)
	at com.aspose.pdf.internal.p145.z7.m1(Unknown Source)
	at com.aspose.pdf.internal.p145.z7.m4(Unknown Source)
	at com.aspose.pdf.internal.p330.z3.m1(Unknown Source)
	at com.aspose.pdf.internal.p330.z3.m1(Unknown Source)
	at com.aspose.pdf.internal.p327.z5.m1(Unknown Source)
	at com.aspose.pdf.internal.p327.z5.m1(Unknown Source)
	at com.aspose.pdf.internal.p363.z1.m1(Unknown Source)
	at com.aspose.pdf.z36.m1(Unknown Source)
	at com.aspose.pdf.ADocument.m1(Unknown Source)
	at com.aspose.pdf.ADocument.<init>(Unknown Source)
	at com.aspose.pdf.Document.<init>(Unknown Source)

and any subsequent attempts to convert the html text to pdf result in a NoClassDefFoundError, which makes sense since the JVM crashed.

Anyway, here is the system info (the result of uname -a):

Linux 2.6.32-504.el6.x86_64 #1 SMP Wed Oct 15 04:27:16 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

and here is the jdk info:

openjdk version “1.8.0_141”
OpenJDK Runtime Environment (build 1.8.0_141-b16)
OpenJDK 64-Bit Server VM (build 25.141-b16, mixed mode)

Thanks for the help,

Michael

and here is the code snippet:

            HtmlLoadOptions htmlLoadOptions = new HtmlLoadOptions();
            PageInfo pageInfo = new PageInfo();
            
            pageInfo.setHeight(712.0);
            pageInfo.setWidth(692.0);
            
            MarginInfo marginInfo = new MarginInfo();
            marginInfo.setTop(72.0);
            marginInfo.setLeft(72.0);
            
            pageInfo.setMargin(marginInfo);
            htmlLoadOptions.setPageInfo(pageInfo);         
            
            // Provide input stream which is read from to create intermediate document object
            content = "<html><head></head><body>This is a <b>test</b><br/><h2>Ok?</h2></body></html>";
            byte[] bytes = content.getBytes("UTF-8");
            InputStream inputHtmlStream = new ByteArrayInputStream(bytes);
            Document doc = new Document(inputHtmlStream, htmlLoadOptions);
                        
            // Provide output stream to which document object representing a pdf is written to
            ByteArrayOutputStream outputPdfStream = new ByteArrayOutputStream();
            doc.save(outputPdfStream);
            
            return outputPdfStream.toByteArray();

The page size and margins are displayed correctly in the pdf when I run this locally. On my machine no error happens and the pdf is generated successfully. As I mentioned above this issue only happens on the remote box, whose configuration I provided in the last comment.

The OutOfMemoryError happens on line

            Document doc = new Document(inputHtmlStream, htmlLoadOptions);

Finally as a point of comparison, my local machine has the following:

java version "1.8.0_92"
Java(TM) SE Runtime Environment (build 1.8.0_92-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.92-b14, mixed mode)

and it’s a 64 bit machine running on the Darwin kernel v15.4.0

Update

Okay so one more update. I managed to fix the OutOfMemoryError on the server by increasing the heap size for the jvm to from 64 MB to 256 MB. Locally things were fine because my default max heap size was a fourth of my ram, so 4 GB. However, while this issue is gone now, I am seeing a new error being thrown:

java.lang.ExceptionInInitializerError: null
	at com.aspose.pdf.internal.p564.z16.m3(Unknown Source)
	at com.aspose.pdf.internal.p564.z16.<clinit>(Unknown Source)
	at com.aspose.pdf.internal.p322.z11.m1(Unknown Source)
	at com.aspose.pdf.internal.p322.z11.m1(Unknown Source)
	at com.aspose.pdf.internal.p322.z8.m1(Unknown Source)
	at com.aspose.pdf.internal.p322.z10.m1(Unknown Source)
	at com.aspose.pdf.internal.p322.z10.m1(Unknown Source)
	at com.aspose.pdf.internal.p322.z10.m1(Unknown Source)
	at com.aspose.pdf.internal.p202.z2.m1(Unknown Source)
	at com.aspose.pdf.internal.p201.z10.m1(Unknown Source)
	at com.aspose.pdf.internal.p213.z3.m1(Unknown Source)
	at com.aspose.pdf.internal.p185.z33.<init>(Unknown Source)
	at com.aspose.pdf.internal.p185.z5.m1(Unknown Source)
	at com.aspose.pdf.internal.p185.z5.<init>(Unknown Source)
	at com.aspose.pdf.internal.p212.z23.m1(Unknown Source)
	at com.aspose.pdf.internal.p192.z13.<init>(Unknown Source)
	at com.aspose.pdf.internal.p184.z4.m4(Unknown Source)
	at com.aspose.pdf.internal.p184.z4.<init>(Unknown Source)
	at com.aspose.pdf.internal.p184.z5.m1(Unknown Source)
	at com.aspose.pdf.internal.p180.z19.m1(Unknown Source)
	at com.aspose.pdf.internal.p180.z19.m1(Unknown Source)
	at com.aspose.pdf.internal.p363.z1.m1(Unknown Source)
	at com.aspose.pdf.internal.p363.z1.m1(Unknown Source)
	at com.aspose.pdf.z36.m1(Unknown Source)
	at com.aspose.pdf.ADocument.m1(Unknown Source)
	at com.aspose.pdf.ADocument.<init>(Unknown Source)
	at com.aspose.pdf.Document.<init>(Unknown Source)

Note that I have synchronized the heap sizes on my remote and my local machine, both are using JVMs with a max heap size of 256 MB. This error is only happening on the remote instance. The code samples and everything else you can get from my previous posts.

@mkochu,
Based on the initial investigation, we are unable to replicate the said error in our environment. We can convert your HTML document to PDF. Kindly try to adjust the limit of minimum and maximum JVM heap size in the problematic environment. The ExceptionInInitializerError is thrown when the JVM attempts to load a new class. During the class loading procedure, all static variables and static initializers are being evaluated.

Best Regards,
Imran Rafique

Can you give me any clue as to why that java.lang.ExceptionInInitializerError: null
is happening?

Even if you cannot reproduce it, it would be great to get an idea of what might be happening aside from a generic description of what an ExceptionInInitializerError is, which I got from the Java docs. I don’t know how to proceed since Aspose is entirely closed source (hence all the Unknown Source) in the stack trace.

Typically the ExceptionInInitalizerError is caused by an exception thrown in a static block initializer, which the error describes, so something like

Exception in thread "main" java.lang.ExceptionInInitializerError
        at obd2ner.main(obd2ner.java:26)
Caused by: java.lang.ClassCastException

The key point is the ‘Caused by:’ is not seen because all of your code is proprietary. I understand you cannot share this code with me. What I am asking is for you to look at the code yourself, if that is possible, and let me know what could have caused the ExceptionInInitializerError.

That would greatly aid in debugging this problem.

@mkochu,
We will create a Linux instance to replicate the same error in our environment. It is quite difficult for us to fix an error without replicating it in our environment, but we are preparing the required environment and will get back to you in the next week. Please spare us a little time.

Best Regards,
Imran Rafique

Hi Imran,

Are there any updates?

@mkochu,
We have already tested in an available Linux environment and could not replicate the said error. Environment details:

CentOS 7.0 64-bit
Eclipse Oxygen Release (4.7.0)
JDK 1.8
Aspose.Pdf for Java 17.7

We require little time to prepare a new environment. However, it would be great if you can prepare a Virtual Machine which reproduces this error with the same environment, and then send us a Zip. We will investigate and share our findings with you.

Best Regards,
Imran Rafique

@mkochu,

We have tested your scenario in the Red Hat Enterprise Linux Server 6.6 (Java 1.7.0_65) with Xms64M and Xmx256M. It works fine and does show the out of memory error.