本文介绍了java.lang.NoClassDefFoundError:com / itextpdf / text / DocumentException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过以下servlet生成动态PDF文件。

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";
}
}

但我收到以下错误:

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)

我正在使用 weblogic应用服务器8.1 ....
我正在使用 iTextPDF 即可。所以我为jar文件设置了CLASSPATH。

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.

谢谢

完成建议后我收到以下错误

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) 


推荐答案

之前的回答告诉你一个罐子丢失了,这不是一个糟糕的猜测,因为错误消息清楚地说明了其中一个iText类无法找到。

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.

不幸的是,该错误消息具有误导性。 Java还说如果有任何歧义,它就找不到类。如果您的CLASSPATH中有多个iText jar,就会出现这种情况。

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.

通过在CLASSPATH中添加另一个iText jar,你的问题变得更糟了。现在您遇到的问题是由于您的weblogic实例中的JVM有两个不同版本的iText。

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.

搜索所有CLASSPATH,不要忘记服务器CLASSPATH,你会发现D:\itextpdf-5.3.4.jar不是weblogic寻找PdfWriter类的唯一地方。从CLASSPATH中删除所有iText罐子,直到你只剩下一个。

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.

这篇关于java.lang.NoClassDefFoundError:com / itextpdf / text / DocumentException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 18:16