HTTP Status 500 - javax.servlet.ServletException: java.lang.NoClassDefFoundError: junit/framework/Test解决方法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package webViewer; 2 3 import java.io.*; 4 5 import junit.framework.Test; 6 7 import com.aspose.words.*; //引入espouse-word-14.11.0-jdk16.jar包 8 9 public class Word2Pdf { 10 private static boolean getLicense() { 11 boolean result = false; 12 try { 13 InputStream is = Test.class.getClassLoader().getResourceAsStream("wordlicense.xml"); // license.xml应放在..\WebRoot\WEB-INF\classes路径下 14 com.aspose.words.License aposeLic = new com.aspose.words.License(); 15 aposeLic.setLicense(is); 16 result = true; 17 } catch (Exception e) { 18 e.printStackTrace(); 19 } 20 return result; 21 } 22 23 public static void word2pdf(String Address) { 24 25 if (!getLicense()) { // 验证License 若不验证则转化出的PDP文档会有水印产生 26 return; 27 } 28 try { 29 File file = new File("C:/inetpub/wwwroot/web/file/pdf1xxxx.pdf"); //新建一个空白pdf文档 30 FileOutputStream os = new FileOutputStream(file); 31 Document doc = new Document(Address); //Address是将要被转化的word文档 32 doc.save(os, SaveFormat.PDF); //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换 33 os.close(); 34 } catch (Exception e) { 35 e.printStackTrace(); 36 } 37 } 38 }
运行代码
1 package webViewer; 2 public class Test { 3 public static void main(String[] args){ 4 Word2Pdf.word2pdf("http://172.16.8.80/file/4.docx"); 5 /*File f=new File("C:/Program Files (x86)/Apache Software Foundation/Tomcat 7.0/webapps/generic/web/file/pdf1.pdf"); 6 if(f.exists()){ 7 f.delete(); 8 } else{ 9 System.out.println("无文件"); 10 }*/ 11 } 12 }
运行以上代码可以正常生成想要的pdf文件
但是将java函数放到jsp中
如下
1 <%@ page language="java" import="java.sql.*" import="java.util.*" import="java.io.*" pageEncoding="utf-8" %> 2 <%@ page import="webViewer.*" %> 3 <%@ page import="com.aspose.words.*" %> 4 <%@ page import="com.aspose.cells.*" %> 5 <% 6 String path = request.getContextPath(); 7 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 8 %> 9 10 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 11 <html> 12 <head> 13 <base href="<%=basePath%>"> 14 <title>My JSP 'View.jsp' starting page</title> 15 <meta http-equiv="pragma" content="no-cache"> 16 <meta http-equiv="cache-control" content="no-cache"> 17 <meta http-equiv="expires" content="0"> 18 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 19 <meta http-equiv="description" content="This is my page"> 20 21 <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> 22 23 </head> 24 <% 25 String pdfFileAddress=request.getParameter("pdfFileAddress"); 26 String suffix=request.getParameter("suffix"); 27 String pdfFileAddress1; 28 %> 29 <body> 30 <% 31 /* File f=new File("C:/Program Files (x86)/Apache Software Foundation/Tomcat 7.0/webapps/generic/web/file/pdf1.pdf"); 32 if(f.exists()){ 33 f.delete(); } 34 else{ */ 35 %> 36 <% 37 if(suffix.equals("doc")||suffix.equals("docx")){ 38 Word2Pdf.word2pdf("http://172.16.8.80/"+pdfFileAddress); 39 40 // Word2Pdf.word2pdf("http://172.16.8.80/file/4.docx"); 41 /* Word2Pdf.word2pdf("http://172.16.8.80/"+pdfFileAddress);*/ 42 /* try { 43 File file = new File("C:/Program Files (x86)/Apache Software Foundation/Tomcat 7.0/webapps/generic/web/file/pdf1.pdf"); 44 FileOutputStream os = new FileOutputStream(file); 45 Document doc = new Document("C:/Program Files (x86)/Apache Software Foundation/Tomcat 7.0/webapps/generic/web/"+pdfFileAddress); 46 doc.save(os, com.aspose.words.SaveFormat.PDF);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换 47 os.close(); 48 } catch (Exception e) { 49 e.printStackTrace(); 50 } */ 51 pdfFileAddress1="file/pdf1xxxx.pdf"; 52 }else{ 53 if(suffix.equals("xls")||suffix.equals("xlsx")){ 54 Excel2Pdf.excel2pdf("http://172.16.8.80/"+pdfFileAddress); 55 pdfFileAddress1="file/pdf1yyyy.pdf"; 56 }/*else{ 57 if(suffix.equals("ppt")||suffix.equals("pptx")){ 58 Ppt2pdf.ppt2pdf("http://172.16.8.80:8080/generic/web/"+pdfFileAddress); 59 pdfFileAddress1="file/pdf1.pdf"; 60 }*/ else{ 61 pdfFileAddress1=pdfFileAddress; 62 } 63 } 64 /*}*/ 65 66 %> 67 <iframe src="http://172.16.8.80/viewer.html?file=<%=pdfFileAddress1%>" height="600px" width="680px"></iframe> 68 </body> 69 </html>
页面会出现以下错误
HTTP Status 500 - javax.servlet.ServletException: java.lang.NoClassDefFoundError:
junit/framework/Test
type Exception report
message javax.servlet.ServletException: java.lang.NoClassDefFoundError:
junit/framework/Test
description The server encountered an internal error that prevented it from fulfilling
this request.
exception
org.apache.jasper.JasperException: javax.servlet.ServletException:
java.lang.NoClassDefFoundError: junit/framework/Test
org.apache.jasper.servlet.JspServletWrapper.handleJspException
(JspServletWrapper.java:556)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:462)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
javax.servlet.ServletException: java.lang.NoClassDefFoundError: junit/framework/Test
org.apache.jasper.runtime.PageContextImpl.doHandlePageException
(PageContextImpl.java:916)
org.apache.jasper.runtime.PageContextImpl.handlePageException
(PageContextImpl.java:845)
org.apache.jsp.View_jsp._jspService(View_jsp.java:179)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.NoClassDefFoundError: junit/framework/Test
webViewer.Word2Pdf.getLicense(Word2Pdf.java:13)
webViewer.Word2Pdf.word2pdf(Word2Pdf.java:25)
org.apache.jsp.View_jsp._jspService(View_jsp.java:133)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.70
logs.
Apache Tomcat/7.0.70
经上网研究,解决方法是:tomcat的lib库中缺少junit.jar包
涨到Eclipse中junit.jar包 如图:
将该jar包拷贝到tomcat的lib目录中 如下图
最后重新启动tomcat即可正常生成pdf。