java.lang.NoClassDefFoundError: com/itextpdf/text/DocumentException
Asked Answered
H

7

18

I am trying to generate a dynamic PDF file through the following servlet.

import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
// Document Object
import com.itextpdf.text.Document;
//For adding content into PDF document
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.DocumentException;

public class CreatePDFExample extends HttpServlet {

    //invoked from doGet method to create PDF through servlet 
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    //Set content type to application / pdf
    //browser will open the document only if this is set
    response.setContentType("application/pdf");
    //Get the output stream for writing PDF object        
    OutputStream out=response.getOutputStream();
    try {
        Document document = new Document();
        /* Basic PDF Creation inside servlet */
        PdfWriter.getInstance(document, out);
        document.open();
        document.add(new Paragraph("Tutorial to Generate PDF using Servlet"));
        document.add(new Paragraph("PDF Created Using Servlet, iText Example Works"));
        document.close();
    }
            catch (DocumentException exc){
            throw new IOException(exc.getMessage());
            }
    finally {            
        out.close();
    }
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

@Override
public String getServletInfo() {
    return "This Servlet Generates PDF Using iText Library";
}
}

but I receive the following error:

Error 500--Internal Server Error

java.lang.NoClassDefFoundError: com/itextpdf/text/DocumentException
at CreatePDFExample.processRequest(CreatePDFExample.java:24)
at CreatePDFExample.doPost(CreatePDFExample.java:47)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:1053)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:387)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:305)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:6310)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:317)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:118)
at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:3622)
at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2569)
at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:197)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:170)

I am using weblogic application server 8.1.... I am using iTextPDF. so I have set the CLASSPATH for the jar files.

CLASSPATH:
D:\itextpdf-5.3.4.jar;D:\servlet-2-3.jar;.;

PATH:
C:\Program Files (x86)\Java\jdk1.6.0_14\bin;.;

Please tell me why I am getting this error????I have spent a lot of time for this.Not getting the small problem.Please help me in this.

Thank you

After doing the suggested things.I get the following error

 Error 500--Internal Server Error

 java.lang.ExceptionInInitializerError
at com.itextpdf.text.pdf.PdfWriter.(PdfWriter.java:1403)
at CreatePDFExample.processRequest(CreatePDFExample.java:26)
at CreatePDFExample.doPost(CreatePDFExample.java:47)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:1053)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:387)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:305)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:6310)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:317)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:118)
at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:3622)
at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2569)
at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:197)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:170)
 Caused by: java.lang.NullPointerException
at java.lang.Class.privateGetDeclaredFields(Class.java:1488)
at java.lang.Class.getDeclaredFields(Class.java:1073) 
Haden answered 3/1, 2013 at 12:5 Comment(1)
Where have you set the classpath? The weblogic startup script?Pastore
D
22

The previous answer told you that a jar was missing, which was not a bad guess because the error message clearly says one of the iText classes couldn't be found.

Unfortunately, that error message is misleading. Java also says it can't find a class if there's any ambiguity. This is the case if you have more than one iText jar in your CLASSPATH.

You've made the problem worse by adding yet another iText jar to your CLASSPATH. Now you have a problem that is caused by having two different versions of iText available for the JVM in your weblogic instance.

Search all the CLASSPATHs, don't forget the server CLASSPATH, and you'll discover that D:\itextpdf-5.3.4.jar isn't the only place where weblogic goes looking for the PdfWriter class. Remove all iText jars from your CLASSPATH until you have only one left.

Duodiode answered 3/1, 2013 at 17:15 Comment(1)
Thanks--this was definitely it. Especially if you're using Maven, and like I did, had iText as an explicit dependency. Excellent advice.Glycogen
B
9

You need to put in the classpath of your web application and not your systems classpath.

And the easiest way to put the jar files on the classpath so that your server can get it at run time is :

PUT THE itextpdf-5.3.4.jar inside the

YOUR_WEBAPP_ROOT --> WEB-INF --> lib folder

so now your jar file should appear here

YOUR_WEBAPP_ROOT --> WEB-INF --> lib --> itextpdf-5.3.4.jar

Barghest answered 3/1, 2013 at 12:13 Comment(2)
still I am getting an error: java.lang.ExceptionInInitializerError at com.itextpdf.text.pdf.PdfWriter.(PdfWriter.java:1403) at CreatePDFExample.processRequest(CreatePDFExample.java:26)Haden
the code shown above is the code.In the code PdfWriter is a class and getInstance() is a static method of the same.Haden
M
5

Add your libraries to your war, inside WEB-INF/lib folder.

Marbles answered 3/1, 2013 at 12:10 Comment(0)
A
2

In my case, iText v.2.1.7 worked, I tried 5.5.3 and 5.5.4 with no luck.

An excerpt from Primefaces V.5.0 user guide, p.12 "Dependencies"

"Listed versions(itext 2.1.7, apache poi 3.7) are tested and known to be working with PrimeFaces, other versions of these dependencies may also work but not tested."

Ambrogio answered 22/12, 2014 at 16:33 Comment(0)
B
2

In my case y use this in my pom.xml

<dependency>
    <groupId>com.lowagie</groupId>
    <artifactId>itext</artifactId>
    <version>2.1.7</version>
</dependency>
Blaeberry answered 25/10, 2019 at 2:58 Comment(0)
A
0

I was having a similar issue. On one system it pdf generation was working fine and another system I was getting this exception. After doing a bit of investigation I found that th working server had the itext-2.1.7.js6.jar with size 1105KB and the one not working had 12Kb. I replaced the jar with one with small size and it started working fine. Not sure how I ended up with same version jar with two different sizes. Hope this helps

Acroter answered 20/7, 2018 at 5:44 Comment(0)
M
-1

I have simple action to solve your mistake you have to make a new "plugin from existing jar file" you integrated this one in your run configurator and final use this plugin like librairie i check this solution and it work without probleme

Marquittamarr answered 21/12, 2015 at 23:2 Comment(1)
This answer is ambiguous at best. Provide some details for the deceptively simple sounding "make a plugin from existing .jar file". It sounds like you are referring to some form of IDE functionality perhaps - if so, what IDE?Sella

© 2022 - 2024 — McMap. All rights reserved.