user3043180
user3043180

Reputation: 11

JasperReports: calling report from jar

I'm using JasperReports 4.7.1 plugin for NetBeans 7.2 to generate report from mysql database and while I run the application from the ide there is no problem found accept this warnings:

log4j:WARN No appenders could be found for logger (net.sf.jasperreports.engine.xml.JRXmlDigesterFactory).
log4j:WARN Please initialize the log4j system properly.

But the report generated and viewed correctly.

The problem is while I clean and build the application then rung it from the jar file the report does not generated and does not give me any exceptions just no reports viewed and every thing else is normal?

This is the function I use for viewing the report in JasperViewer:

public void printInvoice(int invid) throws IOException {
    try {
        String sql = "SELECT\n" +
                "     ordersdetails.`ITEMNAME` AS ordersdetails_ITEMNAME,\n" +
                "     ordersdetails.`AMOUNT` AS ordersdetails_AMOUNT,\n" +
                "     ordersdetails.`PRICE` AS ordersdetails_PRICE,\n" +
                "     invoices.`INVOICEID` AS invoices_INVOICEID,\n" +
                "     invoices.`CUSTOMER` AS invoices_CUSTOMER,\n" +
                "     invoices.`THEDATE` AS invoices_THEDATE,\n" +
                "     invoices.`COST` AS invoices_COST\n" +
                "FROM\n" +
                "     `invoices` invoices RIGHT OUTER JOIN `ordersdetails` ordersdetails ON invoices.`INVOICEID` = ordersdetails.`INVOICE` where invoices.invoiceid=" + invid;

        InputStream in = this.getClass().getResourceAsStream("/reports/invoice.jrxml");

        JasperDesign jd = JRXmlLoader.load(in);

        JRDesignQuery q = new JRDesignQuery();
        q.setText(sql);
        jd.setQuery(q);

        JasperReport jasp_rep = JasperCompileManager.compileReport(jd);

        JasperPrint jasp_print = JasperFillManager.fillReport(jasp_rep, null, mc.getConnection());
        JasperViewer.viewReport(jasp_print, false);
    } catch (JRException e) {
        JOptionPane.showMessageDialog(null, e.getMessage());
        System.out.println(e);
    }
}

Upvotes: 1

Views: 1232

Answers (2)

Do not run the jar file directly. Instead, run it from the command line using java -jar myjar.jar as @PeterMmm has said in the comments above and you will see the errors listed. Try to find the error from that. I think the error is probably a NoClassDefFoundError and it is due to a wrong version of a library file. If that is the reason, download a correct version of the library jar file which contain the missing class definitions and add it to the project library and build the project.

Upvotes: 1

PeterMmm
PeterMmm

Reputation: 24630

Propably load the report from the wrong location, try relative path:

InputStream in = this.getClass().getResourceAsStream("reports/invoice.jrxml");

That means there is the invoice.jrxml in the report folder and that folder is at same level as the class (this.getClass()) that invokes getResourceAsStream.

Additionaly you can get around the warning with

org.apache.log4j.BasicConfigurator.configure();

at beginning of your program.

Upvotes: 1

Related Questions