FileNotFoundException when wanting to run a report made with iReport

-4

When wanting to run a report made with iReport (the first one I do), it throws me the following error FileNotFoundException. I understand that it does not find the way to the file, but it is not clear to me how specify it, try in several ways, here are the last ones, so if anyone can tell me what I'm doing wrong. First Attempt:

        try {
        String compiledReport = "d:\java\movimientosporfecha.jasper";
        JasperReport report = (JasperReport) JRLoader.loadObjectFromFile(compiledReport);

        Map parametros = new HashMap();

        parametros.put("pdefec", this.defec);
        parametros.put("phafec", this.hafec);

        JasperPrint j = JasperFillManager.fillReport(report, parametros, connection);
        JasperViewer jv = new JasperViewer(j, false);
        jv.setTitle("Reporte de Movimientos por Fecha");
        jv.setVisible(true);
  } catch (Exception e){
        System.out.println(e);           
    }

Second Attempt:

try {

        String compiledReport = "d:\java\movimientosporfecha.jasper";
        URL in = this.getClass().getResource(compiledReport); 
        JasperReport report = (JasperReport) JRLoader.loadObject(in);

        Map parametros = new HashMap();

        parametros.put("pdefec", this.defec);
        parametros.put("phafec", this.hafec);

        JasperPrint j = JasperFillManager.fillReport(report, parametros, connection);
        JasperViewer jv = new JasperViewer(j, false);
        jv.setTitle("Reporte de Movimientos por Fecha");
        jv.setVisible(true);
    } catch (Exception e){
        System.out.println(e);
    }

In both cases, it sends me the same error!

The report exists in the folder!

And also try different ways to specify the route, for example:

String compiledReport="d: \ java \ movementsbydate.jasper";

String compiledReport="d: //java//movimientosporfecha.jasper";

Either by specifying the full path to the folder of the reports in the project: "D: //Java//GastosJSF2//src//main//java//reportes//movimientosporfecha.jasper"

Never Works!

    
asked by Fernando 06.10.2016 в 00:46
source

1 answer

1

In Java there is a way to get the directory in which you are working which is easier when working with files that are within the project folders, one of the options are the following:

  

System.getProperty ("user.dir");   new File ("."). getCanonicalPath ();

so your variable would be as follows:

  

String compiledReport = System.getProperty ("user.dir") + "\ src \ main \ java \ reports \ movementsbydate.jasper";

     

String compiledReport = new File ("."). getCanonicalPath () + "\ src \ main \ java \ reports \ movementsbydate.jasper";

Another recommendation would be that you would put your report folder at the root of the project if you get more information about the route, as follows:

  

"D: //Java//GastosJSF2//reportes//movimientosporfecha.jasper"

in this way the following variables would remain:

  

String compiledReport = System.getProperty ("user.dir") + "\ reports \ movementsbydate.jasper";   String compiledReport = new File ("."). GetCanonicalPath () + "\ reports \ movementsbydate.jasper";

    
answered by 06.10.2016 в 01:32