JasperReport, show and print report

I exported a .jrprint file created with iReport. Now I want to preview the report and finally print it, how can I do this? I'm trying with:

JRBeanCollectionDataSource ds=new JRBeanCollectionDataSource(list); JasperPrint jrprint=JasperFillManager.fillReport("report.jrprint", null, ds); 
But I have this exception
java.lang.ClassCastException: net.sf.jasperreports.engine.JasperPrint cannot be cast to net.sf.jasperreports.engine.JasperReport 
22.7k 19 19 gold badges 113 113 silver badges 242 242 bronze badges asked Nov 24, 2010 at 9:48 13.1k 25 25 gold badges 77 77 silver badges 115 115 bronze badges

4 Answers 4

You're specifying the JasperPrint file and not the JasperReport file. Let me break down the files and what they are:

Here's some code to start with the jrxml file the designer creates to get you to an printed pdf output:

Connection connection = PersistenceSessionFactory.getSqlSession().getConnection(); JasperReport report = JasperCompileManager.compileReport( "FancyPantsReport.jrxml" ); // setup parameters for use with the report HashMap params = new HashMap(); params.put( "sqlDate", fromDate ); // Fill the report data from the sql connection and parameters JasperPrint printedReport = JasperFillManager.fillReport(report, params, connection); String outputFilename = "FancyPants-" + dateString + ".pdf"; JasperExportManager.exportReportToPdfFile( printedReport, outputFilename ); LOG.info("Report Generated in " + (System.currentTimeMillis() - start) + "ms"); 

Notice it uses the compile to get a JasperReport from the jrxml, then the FillManager to get a JasperPrint from the JasperReport, and finally exports the JasperPrint to pdf.