4

I am new to jasper, I want to compile the report in jrxml and export to pdf in from absolute path to relative path. Currently the codes work only in absolute path.

export to pdf = download folders of web browser jrxml inside the /Reports/ConsumptionReport.jrxml (inside the web pages) thank you :D

public void showReport(int productionNumber) throws JRException {

        try {




            DBConnectionFactory myFactory = DBConnectionFactory.getInstance();
            Connection conn = myFactory.getConnection();

            Map map = new HashMap();
            map.put("prodNum", productionNumber);

            JasperReport jr = JasperCompileManager.compileReport("/Users/user/NetBeansProjects/EGMI/web/Reports/ConsumptionReport.jrxml");
            //Fill the report with parameter, connection and the stream reader     
            JasperPrint jp = JasperFillManager.fillReport(jr, map, conn);

            JasperExportManager.exportReportToPdfFile(jp, "/Users/user/NetBeansProjects/EGMI/web/Reports/ConsumptionReport.pdf");
            JasperViewer.viewReport(jp, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

folder hierarchy

EGMI
   ---Web Pages
        ----Reports
               -----ConsumptionReport.jrxml 

SOLUTION- servlet

String relativeWebPath = "/Reports";
           String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath);
           File f = new File(absoluteDiskPath, "ConsumptionReport.jrxml");
Zoe
  • 27,060
  • 21
  • 118
  • 148
SCS
  • 1,162
  • 3
  • 17
  • 31

1 Answers1

1

Use java.io.File to get the absolute path from a relative path. es.

File f = new File("yourRelativePath/ConsumptionReport.jrxml");
JasperReport jr = JasperCompileManager.compileReport(f.getAbsolutePath());  

Seeing the problem to find the relative path of your deployed web application I suggest you check out these questions.

Need to find the web application path

What does servletcontext.getRealPath("/") mean and when should I use it

Community
  • 1
  • 1
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
  • I changed it to File f = new File("Reports/ConsumptionReport.jrxml"); JasperReport jr = JasperCompileManager.compileReport(f.getAbsolutePath()); but the problem now is it is getting the wrong path – SCS Oct 26 '15 at 09:33
  • You need to know from which relative path you start... try System.out.println(f.getAbsolutePath()) to understand where you are...and what you are pointing to. – Petter Friberg Oct 26 '15 at 09:33
  • ava.io.FileNotFoundException: /Applications/NetBeans/glassfish-4.1/glassfish/domains/domain1/config/Reports/ConsumptionReport.jrxml this is the error, is there a way to locate the relative path first? – SCS Oct 26 '15 at 09:34
  • the start of your relative path is: /Applications/NetBeans/glassfish-4.1/glassfish/domains/domain1/config/ it is set when applications start... – Petter Friberg Oct 26 '15 at 09:35
  • `File f = new File("/Applications/NetBeans/glassfish-4.1/glassfish/domains/domain1/config/EGMI/web/Reports/ConsumptionReport.jrxml");` i tried this one but it is still not working, I will update my folder hierarchy in the question. thank you :)) – SCS Oct 26 '15 at 09:38
  • Where are your reports?, in which folder?? – Petter Friberg Oct 26 '15 at 09:40
  • i added the folders hierarchy above @petter – SCS Oct 26 '15 at 09:41
  • Add full path to reports...EGMI on your macchine in which folder is?, which is the correct absolute path?? /App.../domains/domani1/EGMI ? – Petter Friberg Oct 26 '15 at 09:48
  • Try this System.out.println(request.getContextPath()) and tell me what you got.. you need to try where you hve the request... – Petter Friberg Oct 26 '15 at 10:00
  • /Users/User/NetBeansProjects/EGMI this one? I dont know where does application/netbeans... is coming from – SCS Oct 26 '15 at 10:00
  • Its the path of the glass fish application that you run... To get the path of your deployed web application is different probably request.getContextPath() or similar can help you – Petter Friberg Oct 26 '15 at 10:03
  • I edit my post with some links on how to get the path to your deployed application, then just work your way to the report. – Petter Friberg Oct 26 '15 at 10:15