java.lang.NoClassDefFoundError in Jasper Report

2019-08-31 07:17发布

问题:

i have created one jasper report in iReport-4.5.0

But now i have to view this report in my java web application which is in Spring,

here is my java file code as,

/***********************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");
}

and my jsp file is as,

<%@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>

i have build it well and run on browser but it gives following errors...

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)

I have also added jar libraries files for Jasper Report in my web application project.

so please help me , Thanks in advance.....

回答1:

I have solved this problem myself.

There is only one silly mistake of jar files of Jasper Report.

I have solved this by adding jar files directly in lib instead of place in build path of eclipse.



回答2:

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();
    }
}