Search code examples
javajsfjasper-reportstomcat7

Creating a pdf Jasper Report, terminates the server execution


I just implemented a Jasper report to generate a pdf document from a java web app running on tomcat 7, but when the report is generated on my pc appears a java icon (img1.png) if I close that "java application" it stops my tomcat server and the execution of my web application, this is the code of my pdf generator method:

.xhtml button:

<h:form>
     <h:commandButton actionListener="#{menuController.jasperTestOne}" value="Jasper PDF" />
 </h:form>

Backend java code:

public void jasperTestOne() {
        try {
            Map<String,Object> parametros= new HashMap<String,Object>();
            parametros.put("PRINT_BY", "SomeUser");

            String jPath = "/Users/Documents/pdf/jasper/test.jasper";
            File jasper = new File(jPath);
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasper.getPath(),parametros, new JRBeanCollectionDataSource(this.getItems()));

            HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
            response.addHeader("Content-disposition","attachment; filename=jsfReporte.pdf");
            ServletOutputStream stream = response.getOutputStream();

            JasperExportManager.exportReportToPdfStream(jasperPrint, stream);

            stream.flush();
            stream.close();
            FacesContext.getCurrentInstance().responseComplete();
        } catch(Exception ex) {
            System.out.println("JASPER ERROR: " + ex.getMessage());
        }
    }

Solution

  • The "java application" is the Tomcat process. OSX starts displaying an icon in the dock because JasperReports uses some graphical resources to measure texts while generating the report.

    Creating the PDF does not terminate the server process, the process terminates when you close the application from the dock. If you leave the application in the dock Tomcat will continue to run.

    If you don't want to see the Tomcat process in the dock you can add -Djava.awt.headless=true to the Tomcat Java options.