如何修复FileNotFoundException

时间:2013-10-03 02:35:04

标签: java jasper-reports

这是我打印发票页面的源代码。我的报告位于 Java 包中。我将它保存在一个名为“report”的文件夹中。

    try {

        String date1 = new SimpleDateFormat("yyyy-MM-dd").format(isdate.getDate());
        String time1 = istime.getValue().toString().split(" ")[3];
        date1 = date1 + " " + time1;

        String date2 = new SimpleDateFormat("yyyy-MM-dd").format(redate.getDate());
        String time2 = retime.getValue().toString().split(" ")[3];
        date2 = date2 + " " + time2;

        JRTableModelDataSource dataSource = new JRTableModelDataSource(jTable1.getModel());
        String reportsource = " D:/Catering/report/report1.jrxml";
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("inid", txtInvoiceID.getText());
        params.put("cuname", txtCuName.getText());
        params.put("cuadd", txtCuid.getText());
        params.put("cutp", txtTPNo.getText());
        params.put("isdate", date1);
        params.put("redate", date2);
        params.put("advance", txtAdvance.getText());
        params.put("due", txtDue.getText());
        params.put("total", txtGtotal.getText());
        JasperReport jasperReport = JasperCompileManager.compileReport(reportsource);
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource);
        JasperViewer.viewReport(jasperPrint, true);
        JOptionPane.showMessageDialog(null, "Done");
    } catch (Exception e) {
       JOptionPane.showMessageDialog(null, "jasper error"+e);
    }

1 个答案:

答案 0 :(得分:1)

最好使报告成为嵌入式资源,但在可能的情况下,您不应该依赖绝对路径,因为这些路径可能会在安装之间发生变化......

尝试更改

String reportsource = " D:/Catering/report/report1.jrxml";
                       ^---- Did you notice the white space here?

String reportsource = "report/report1.jrxml";

通常,预编译报告通常会更好,因此您无需在运行时进行报告...

您可以编写一个简单的程序,使用类似......

之类的东西为您编译它
String templateFile = "report/report1.jrxml"
String compiledReport = "report/report1.jasper"
JasperCompileManager.compileReportToFile(templateFile, compiledReport);

该方法需要两个String个,基本上一个是jrxml文件,另一个是预期的jasper文件。

您可以使用JasperReports' Ant task并将构建过程分开。

或者你可以使用iReports ......

编译完成后,您只需在运行时加载jasper文件...

String compiledReport = "report/report1.jasper";
JasperReport report = (JasperReport)JRLoader.loadObjectFromFile(compiledReport );
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource);

已更新为快速编译示例

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;

public class QuickCompile {

    public static void main(String[] args) {
        try {
            String template = "...";
            String compiled = "...";
            JasperCompileManager.compileReportToFile(template, compiled);

            // Just as a test...
            JasperReport jr = (JasperReport) JRLoader.loadObjectFromFile(compiled);            

        } catch (JRException exp) {
            exp.printStackTrace();
        }            
    }

}