我有一个servlet
使用另一个 class
命名Converter
,这个类使用了我放入 WEB-INF/lib
中的一些外部 jar。但当尝试使用此类时,我仍然得到 java.lang.ClassNotFoundException
,我在这里尝试了无数的解决方案,但仍然没有一个有效:
- 将 jar 放入
classpath
. - 将 jar 放在 WEB-INF/classes 下
它们都不起作用,这是我的 servlet
中的相关部分:
private Converter htmlCon = new Converter(webInfPath);
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
StringBuffer jb = new StringBuffer();
String line = null;
try {
BufferedReader reader = request.getReader();
while ((line = reader.readLine()) != null) {
jb.append(line);
}
} catch (IOException IOE) {
IOE.printStackTrace();
}
try {
htmlCon.createPdf(jb.toString(), "pdf.pdf");
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
和Converter
:
package com.mataf.converters;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
//the external jars
import org.xhtmlrenderer.pdf.ITextRenderer;
import com.lowagie.text.DocumentException;
public class Converter {
private Types m_type;
private String m_pathToCreateFileIn;
public Converter(String i_path) {
this.m_pathToCreateFileIn = i_path;
}
public void createPdf(String html, String fileName) throws IOException, DocumentException{
ITextRenderer renderer = new ITextRenderer();
// if you have html source in hand, use it to generate document object
renderer.setDocumentFromString( html);
renderer.layout();
String fileNameWithPath = m_pathToCreateFileIn + File.separator + "PDF-FromHtmlString.pdf";
FileOutputStream fos = new FileOutputStream( fileNameWithPath );
renderer.createPDF( fos );
fos.close();
System.out.println( "File 2: '" + fileNameWithPath + "' created." );
System.out.println(html);
System.out.println(fileName);
}
}
完整stacktrace
:
com.ibm.ws.webcontainer.servlet.ServletWrapper run SRVE8052E: Logging ClassNotFoundException
java.lang.ClassNotFoundException: class java.lang.NullPointerException: null
at java.beans.Beans.instantiate(Beans.java:190)
at java.beans.Beans.instantiate(Beans.java:75)
at com.ibm.ws.webcontainer.servlet.ServletWrapper$1.run(ServletWrapper.java:1461)
at com.ibm.ws.security.util.AccessController.doPrivileged(AccessController.java:118)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.loadServlet(ServletWrapper.java:1450)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.load(ServletWrapper.java:1348)
at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:980)
at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3703)
at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:304)
at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:962)
at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1662)
at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:195)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:458)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:522)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:311)
at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:87)
at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1783)
最佳答案
请参阅此链接以了解您所看到的异常的原因:Odd behavior in WAS 7.0: java.lang.ClassNotFoundException: class java.lang.NullPointerException: null
“WebSphere Application Server 中包含的 Java 6 版本丢失了对 Java 错误 4256569 的修复,这掩盖了失败的实际原因。”
如果您更新了 websphere 应用程序中的 Java 版本,那么问题的实际原因应该很明显。
关于java - Servlet - 包括来自另一个类的其他 jar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44180935/