1

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);
    }
}
Alex K
  • 22,315
  • 19
  • 108
  • 236
  • You should follow this question: http://stackoverflow.com/questions/8943661/please-initialize-the-log4j-system-properly-warning – Gilberto Torrezan Nov 27 '13 at 19:36
  • the warnings not the problem the problem is from this method to the end JRXmlLoader.load() nothing work when using the compiled jar – user3043180 Nov 27 '13 at 19:51
  • Yes, the warning are just warnings... but in production that means that nothing is actually being logged. So that's a good reason why you don't get any errors. – Gilberto Torrezan Nov 27 '13 at 19:53
  • could you just till me where to put this line log4j.rootLogger=DebugAppender that was in the question you pointed me to – user3043180 Nov 27 '13 at 19:54
  • I think you're new to log4j or log libraries in general. I recommend you to read [this](http://logging.apache.org/log4j/1.2/faq.html#noconfig) and [this](http://logging.apache.org/log4j/1.2/manual.html#Default_Initialization_Procedure) first. – Gilberto Torrezan Nov 27 '13 at 20:05
  • When you look into the jar what is the absolute path of invoice.jrxml and your class ? – PeterMmm Nov 28 '13 at 06:51
  • same problem was there for me while using netbeans, i dont know what was the issue with netbeans probably the classes were not found provided by Jasper Library at runtime while executing the jar. but it was solved easily when i exported the executable jar from eclipse so i recommend you to export your project in eclipse and than export it as executable jar file...problem will be 100% solved. :) – Not a bug Nov 28 '13 at 07:28

2 Answers2

1

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.

PeterMmm
  • 24,152
  • 13
  • 73
  • 111
  • I have changed the path several times and made a check to see if the file is found with a message dialog , the file found and the instructions stops just before this method JRXmlLoader.load(in); when running the compiled jar but when running from the ide it works correctly – user3043180 Nov 28 '13 at 09:25
  • 1
    Try to catch all Exceptions and print exception message to System.err. Try to run from commandline `java -jar myjar.jar` to see if any RuntimeException is thrown ? Propably running fom NB project with diferent classpath. – PeterMmm Nov 28 '13 at 11:47
1

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.

dilanSachi
  • 562
  • 6
  • 14