Jasper Report中的java.lang.NoClassDefFoundError

时间:2014-02-08 06:28:48

标签: java spring jasper-reports

我在 iReport-4.5.0

中创建了一个jasper报告

但现在我必须在我的java web应用程序中查看此报告,该应用程序位于 Spring

这是我的java文件代码,

/***********************Bill-viewer ..Invoice Page ****************************/
/*****Bill-viewer*****/
@RequestMapping("/Billviewer")
public @ResponseBody
ModelAndView billReport(HttpSession session) {
    System.out.println("In Print.......");
    String invoice = (String) session.getAttribute("invoiceNo");

    System.out.println(invoice);

     try
       {
            Connection conn;
            Statement st;
            Class.forName("com.mysql.jdbc.Driver");
            conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/cjbranchdb","root","root");
            st=conn.createStatement();
           JasperReport jasperReport = JasperCompileManager.compileReport("C:\\Jasper Reports\\BillReport.jrxml");
           JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,new HashMap<Object, Object>(),conn);
           JasperViewer jv = new JasperViewer(jasperPrint);
           jv.show();          

           boolean isExitOnClose = false;

           JasperViewer.viewReport(jasperPrint, isExitOnClose);


           st.close();
           conn.close();



       }
       catch(Exception c)
       {
           JOptionPane.showMessageDialog(null,c.getMessage());
           System.out.println("aaaaaaaaaaaa   "+ c);

       }     


    return new ModelAndView("Billviewer");
}

我的jsp文件是,

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <h2>Hello World!</h2>

</body>
</html>

我已经很好地构建它并在浏览器上运行,但它会出现以下错误......

root cause

java.lang.NoClassDefFoundError: net/sf/jasperreports/engine/JasperCompileManager
com.pis.controller.HomePageController.billReport(HomePageController.java:462)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
              org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)

我还在我的网络应用程序项目中为 Jasper Report 添加了jar库文件。

所以请帮助我,提前谢谢.....

2 个答案:

答案 0 :(得分:0)

我自己解决了这个问题。

Jasper Report的 jar文件只有一个愚蠢的错误。

我通过直接在 lib 中添加jar文件而不是在 eclipse 的构建路径中添加来解决这个问题。

答案 1 :(得分:-1)

protected void doGet(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException {

    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbname","root","root");

        JasperReport jasperreport = null;
        JasperDesign jasperdesign = null;
        Map parameters = new HashMap<>();

        String path = getServletContext().getRealPath("/WEB-INF/");
        jasperdesign = JRXmlLoader.load(path + "/jrxmlfilename.jrxml");
        jasperreport = JasperCompileManager.compileReport(jasperdesign);

        byte[] byteStream = JasperRunManager.runReportToPdf(jasperreport, parameters, con);
        OutputStream outStream = response.getOutputStream();
        response.setContentType("application/pdf");
        response.setContentLength(byteStream.length);

        outStream.write(byteStream, 0, byteStream.length);

    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JRException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}