问题描述
先前的答案告诉您,缺少了一个jar,这不是一个不好的猜测,因为错误消息清楚地表明找不到iText类之一。
不幸的是,该错误消息具有误导性。Java还表示,如果存在任何歧义,它将找不到类。如果您的CLASSPATH中有多个iText jar,就是这种情况。
通过在CLASSPATH中添加另一个iText jar,使问题变得更糟。现在,您遇到了一个问题,该问题是由weblogic实例中的JVM提供两个不同版本的iText引起的。
搜索所有CLASSPATH,不要忘记服务器CLASSPATH,您会发现D:\ itextpdf-5.3.4.jar并不是weblogic寻找PdfWriter类的唯一地方。从CLASSPATH中删除所有iText jar,直到只剩下一个。
解决方法
我正在尝试通过以下servlet生成动态PDF文件。
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,IOException {
processRequest(request,response);
}
@Override
protected void doPost(HttpServletRequest 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。
CLASSPATH:
D:\itextpdf-5.3.4.jar;D:\servlet-2-3.jar;.;
PATH:
C:\Program Files (x86)\Java\jdk1.6.0_14\bin;.;
请告诉我为什么会出现此错误????我已经花了很多时间。没有遇到小问题。请帮助我。
谢谢
完成建议的操作后,出现以下错误
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)