0

I am trying to Compile the jasper reports file (Invoice.jrxml) but I am getting this FileNotFound Exception error

The variable sourceFileName contains the name of the file the file is located in

src/com.medicam.servlets/Invoice.jrxml

Below is my code

String [] prod = request.getParameterValues("prodn[]");
        String [] pkgdate = request.getParameterValues("pkgdate[]");
        String [] manufact = request.getParameterValues("manufact[]");
        String [] exp = request.getParameterValues("exp[]");
        String [] batch = request.getParameterValues("batch[]");
        String [] unit = request.getParameterValues("unit[]");
        String [] qty = request.getParameterValues("qty[]");
        String [] subtot = request.getParameterValues("subtot[]");
        ArrayList<String> prodname = new ArrayList<String>();
        ArrayList<String> packagedate = new ArrayList<String>();
        ArrayList<String> manufactdate = new ArrayList<String>();
        ArrayList<String> exipry = new ArrayList<String>();
        ArrayList<String> batchno = new ArrayList<String>();
        ArrayList<String> unitprice = new ArrayList<String>();
        ArrayList<String> quantity = new ArrayList<String>();
        ArrayList<String> Subtotal = new ArrayList<String>();
        Map<String, Object> param = new HashMap<String, Object>();

        for(int i=0;i<prod.length;i++)
        {
            prodname.add(prod[i]);
            packagedate.add(pkgdate[i]);
            manufactdate.add(manufact[i]);
            exipry.add(exp[i]);
            batchno.add(batch[i]);
            unitprice.add(unit[i]);
            quantity.add(qty[i]);
            Subtotal.add(subtot[i]);
        }
        param.put("prodname", prodname);
        param.put("pkgdate", packagedate);
        param.put("manfdate", manufactdate);
        param.put("expdate", exipry);
        param.put("batch", batchno);
        param.put("unit", prodname);
        param.put("qty", quantity);
        param.put("subtot", Subtotal);
        try {
        String sourceFileName = "Invoice.jrxml";
        JasperReport jasperReport = JasperCompileManager.compileReport(sourceFileName);
        JasperFillManager.fillReportToFile(
                 sourceFileName, param);

        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, param, new JREmptyDataSource());
        JasperViewer.viewReport(jasperPrint, false);
            } 
        catch (Exception e) {
                e.printStackTrace();
            } 

StackTrace

net.sf.jasperreports.engine.JRException: java.io.FileNotFoundException: Invoice.jrxml (No such file or directory)
    at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:214)
    at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:185)
    at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:176)
    at net.sf.jasperreports.engine.JasperCompileManager.compile(JasperCompileManager.java:172)
    at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:459)
    at com.medicam.servlets.GenerateReportForPurchase.doPost(GenerateReportForPurchase.java:111)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:526)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:655)
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.FileNotFoundException: Invoice.jrxml (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:146)
    at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:209)
    ... 29 more
Nishant123
  • 1,968
  • 2
  • 26
  • 44

3 Answers3

2

Obviously the report file can't be found by simple file name.

Try to load is as stream. Like this

InputStream is=this.getClass().getResourceAsStream("/com/medicam/servlets/Invoice.jrxml")
JasperReport jasperReport = JasperCompileManager.compileReport(is);
StanislavL
  • 56,971
  • 9
  • 68
  • 98
1

You need to provide the file name either with full file name or relative to your java current directory. Good alternative to using file name is to use InputStream retrieved via Class#getResourceAsStream. This will continue to work even when you distribute your functionality packed into jar archive. Using the file name on the other hand means you need to distribute the report as a standalone file and you have to take care you provide to correct full or relative file name.

Michal
  • 2,353
  • 1
  • 15
  • 18
0

Please mention the complete path of file like

String sourceFileName = "D:\APPS\Invoice.jrxml";

if so "Invoice.jrxml" exists in APPS folder of D Drive(This is for windows)


String sourceFileName = "/usr/local/Invoice.jrxml";

if so "Invoice.jrxml" exists in local directory (This is for Linux)

dineshA
  • 1
  • 1