0

我想使用 servlet、.jasper 文件创建 pdf 格式的报告,但有一个例外:

HTTP 状态 500 - Servlet 执行引发异常

--------------------------------------------------------------------------------

type Exception report

message Servlet execution threw an exception

description The server encountered an internal error that prevented it from fulfilling     this request.

exception 

javax.servlet.ServletException:Servlet 执行引发异常

根本原因

java.lang.NoClassDefFoundError: net/sf/jasperreports/engine/JasperRunManager one.Patient.doGet(Patient.java:46) javax.servlet.http.HttpServlet.service(HttpServlet.java:621) javax.servlet.http.HttpServlet .service(HttpServlet.java:728)

note Apache Tomcat/7.0.34 日志中提供了根本原因的完整堆栈跟踪。


小服务程序代码是:

package one;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.util.HashMap;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperRunManager;

/**
 * Servlet implementation class Patient
 */
@WebServlet("/Patient")
public class Patient extends HttpServlet implements Servlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Patient() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        Connection con=DBCon.getConnection();
        InputStream inS=getServletContext().getResourceAsStream("/report-src/ganeshaji.jashper");

        OutputStream outS=response.getOutputStream();
        response.setContentType("application/pdf");

        try {
            JasperRunManager.runReportToPdfStream(inS, outS, new HashMap<String, Object>(),con);
            outS.flush();
            con.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            response.setContentType("text/html");
            e.printStackTrace();
        }   
        }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

请。给我解决方案

4

2 回答 2

3

通过调查根本原因

java.lang.NoClassDefFoundError: net/sf/jasperreports/engine/JasperRunManager 

似乎您要么错过了 jasper jar,要么使用了不支持 JasperRunManager 的错误版本。

在类路径中包含正确的 jar 将解决您的问题。

请下载这些罐子:

itext-4.2.0.jar 

itextpdf-5.3.4.jar 

jasperreports-4.0.1.jar 

jasperreports-fonts-4.0.0.jar 

并编写如下代码:

try { 
FileInputStream fis = new FileInputStream(YOURREPORTPATH.jasper"); 
BufferedInputStream bufferedInputStream = new BufferedInputStream(fis); 
jasperReport = (JasperReport) JRLoader.loadObject(bufferedInputStream); 
} catch (Exception e2) {
 e2.printStackTrace(); 
}

然后在填写参数和数据源后创建pdf,如下所示:

JasperExportManager.exportReportToPdfFile(jasperPrint, OUTPUTFILENAME+".pdf");

谢谢...

777先生

于 2013-02-25T12:30:01.627 回答
0

检测到在 NoClassDefFoundError:net/sf/jasperreports/engine/util/JRStyledTextParser 之前抛出的根异常:

java.lang.NoClassDefFoundError: sun/awt/X11GraphicsEnvironment

Unix 和 Linux 上的 Sun AWT 类依赖于 X Window 系统。当您使用这些类时,它们期望加载 X 客户端库并能够与 X 显示服务器通信。如果您的客户端有 GUI,这是有道理的;不幸的是,即使您的客户使用 AWT 但没有 GUI(在我的情况下,从 Web 应用程序生成报告),它也是必需的

绕过这个的方法是java.awt.headless=true在系统启动时设置系统属性。

例如

通过将静态块移动到类的顶部,让 java 尽可能早地执行它!

  public class Foo() {
    static { /* works fine! ! */
      System.setProperty("java.awt.headless", "true");
      }

     public static void main() {}
  }

或者

我认为可以通过将其作为参数传递给 Java 虚拟机来设置此参数,例如

-Djava.awt.headless=true。不确定这是否会对您的应用程序的其他组件产生影响。

或者

试着把它放在你的 ${CATALINA_HOME}/bin/catalina.sh 脚本中:

export JAVA_OPTS="${JAVA_OPTS} -Djava.awt.headless=true"

来自- https://stackoverflow.com/a/3651196/1602230

于 2013-02-25T12:27:26.537 回答