views:

11

answers:

1

My .jrxml file contains 2 text and one param

alt text

Following is my java code for jasper report, When i run the code it creates pdf file but it is always empty any help ?

public class TestReport {

    public static void runReport(String reportFile) {
          try{

            Map parameters = new HashMap();
            parameters.put("my_var", "Address Report");

            JasperDesign jasperDesign = JRXmlLoader.load(reportFile);
            JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
             JasperPrint jsp = JasperFillManager.fillReport(jasperReport, parameters);
             JasperExportManager.exportReportToPdfFile(jsp, "/home/ireports/test1.pdf");

          }catch(Exception ex) {
             String connectMsg = "Could not create the report " + ex.getMessage() + " " + ex.getLocalizedMessage();
             System.out.println(connectMsg);
          }
       }

     public static void main(String args[]){
         runReport("/home/ireports/report1.jrxml");
     }
}
+1  A: 

Try with this snippet. It will work ,Here fileName would be the .jasper file compiled version of .jrxml

       try {
            JasperPrint print = JasperFillManager.fillReport(
                    fileName,
                    hm,
                    new JREmptyDataSource());
            JRExporter exporter =
            new net.sf.jasperreports.engine.export.JRPdfExporter();
            exporter.setParameter(
            JRExporterParameter.OUTPUT_FILE_NAME,
            outFileName);
            exporter.setParameter(
            JRExporterParameter.JASPER_PRINT, print);
            JasperExportManager.exportReportToPdfFile(print, outFileName);
            print = null;
            exporter = null;
            //JasperViewer.viewReport(print,false);
        } catch (Exception e) {
            e.printStackTrace();
        }
org.life.java