java.lang.NoClassDefFoundError: com/itextpdf/text/

2019-02-21 13:33发布

问题:

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) 

回答1:

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.



回答2:

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



回答3:

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



回答4:

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."



回答5:

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



回答6:

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