2

当我在 Netbeans 中运行它时它工作正常,但它不会显示在可执行 jar 文件中这是我的代码

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){                                         
    this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    rep.siapkan_report_dengan_parameter("report4.jrxml", "Report", "periode",tCari.getText());
    this.setCursor(Cursor.getDefaultCursor());
}


public void siapkan_report_dengan_parameter(      
    String nama_report,
    String direktori,
    String namaparameter,
    String isiparameter){      

    konek.openConnection();       

    Properties systemProp = System.getProperties();

    // Ambil current dir
    String currentDir = systemProp.getProperty("user.dir");

    File dir = new File(currentDir);
    String reportName = nama_report;
    String reportDirName = direktori;

    File fileRpt;
    String fullPath = "";
    if (dir.isDirectory()) {
        String[] isiDir = dir.list();
        for (int i = 0; i < isiDir.length; i++) {
            fileRpt = new File(currentDir + File.separatorChar + isiDir[i] + File.separatorChar +
                    reportDirName + File.separatorChar + reportName);
            if (fileRpt.isFile()) { // Cek apakah file  ada 
                fullPath = fileRpt.toString();                  
            } 
        } 
    }         

    String[] subRptDir = fullPath.split(reportName);

    try {

        // Persiapkan parameter untuk Report
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put(namaparameter, isiparameter);
        try {
            JasperReport JRpt = JasperCompileManager.compileReport(fullPath);                
            JasperPrint JPrint = JasperFillManager.fillReport(JRpt, parameters,konek.conn);
            if(JPrint.getPages().isEmpty()){
                JOptionPane.showMessageDialog(null, 
                "Data Untuk Kriteria :\n" + isiparameter +"\nTidak Ada", 
                "Peringatan", 
                JOptionPane.ERROR_MESSAGE);

            }else{
                JasperViewer.viewReport(JPrint, false);                
            }
        } catch (Exception rptexcpt) {
            JOptionPane.showMessageDialog(null, 
                "a", 
                "Peringatan", 
                JOptionPane.ERROR_MESSAGE);
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, 
                "b", 
                "Peringatan", 
                JOptionPane.ERROR_MESSAGE);
    }

}

提前致谢!

我的图书馆是:

  • common-beanutils-1.7
  • commons-collections-2.1
  • commons-digester-1.7
  • commons-javaflow-20060411
  • 公共日志记录-1.1
  • jasperreports-3.0.1
  • jdt-compiler-3.1.1
  • jfreechart-1.0.3
  • poi-3.0.1-FINAL-20070705
4

3 回答 3

0

问题是您没有将 jasper 报告作为流加载。所以,尝试添加这个:

InputStream st = getClass().getResourceAsStream(fullPath);

try {
            JasperDesign jd = JRXmlLoader.load(st);
            JasperReport JRpt = JasperCompileManager.compileReport(jd);                
            JasperPrint JPrint = JasperFillManager.fillReport(JRpt, parameters,konek.conn);
            if(JPrint.getPages().isEmpty()){
                JOptionPane.showMessageDialog(null, 
                "Data Untuk Kriteria :\n" + isiparameter +"\nTidak Ada", 
                "Peringatan", 
                JOptionPane.ERROR_MESSAGE);

            }else{
                JasperViewer.viewReport(JPrint, false);                
            }
        } catch (Exception rptexcpt) {
            JOptionPane.showMessageDialog(null, 
                "a", 
                "Peringatan", 
                JOptionPane.ERROR_MESSAGE);
        }

此外,在将.jrxml文件导出到.jar.

您将通过的路径 (fullPath) 将类似于:/com/report/nameofreport.jrxml.

于 2013-08-06T00:18:55.363 回答
0

在 jar 文件中使用已编译的 jasper 报告效率不高。我建议您从 PC 上的某个位置引用该报告,例如 C:驱动器。

如果您有一个可以轻松访问的永久文件夹,那也是最好的。可能是C:/<project name>/report/<your-report>

这种方法使您能够轻松地分发它。

于 2020-06-03T15:00:35.570 回答
0

问题不在于您的代码...只需添加以下库。不要使用最新的 jasper 库。这就是不通过 jar 可执行文件加载报告的原因。(使用 ireport 5.5.0 库)。我使用的代码如下

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    org.apache.log4j.BasicConfigurator.configure(new NullAppender());
    //InputStream st = getClass().getResourceAsStream("/Reports/SalesInvoiceCustomerCopy.jrxml");
    try
    {
        Connection connect = conn.open();

        String report2 = "H:\\Higher Diploma Project\\FinalHD\\src\\Reports\\SalesInvoiceCustomerCopy.jrxml";
        JasperReport rep2 = JasperCompileManager.compileReport(report2);
        JasperPrint rep_print2 = JasperFillManager.fillReport(rep2,null,connect);
        JasperPrintManager.printReport(rep_print2,false);

        String report = "H:\\Higher Diploma Project\\FinalHD\\src\\Reports\\SalesInvoice.jrxml";
        JasperReport rep = JasperCompileManager.compileReport(report);
        JasperPrint rep_print = JasperFillManager.fillReport(rep,null,connect);
        JasperPrintManager.printReport(rep_print,false);

        try
        {
            pst =conn.open().prepareStatement("INSERT INTO invoice_balanace VALUES (?,?,?,?)") ;
            pst.setString(1, txtInvoiceNo.getText());
            pst.setString(2, txtValue.getText());
            pst.setString(3, totDisc.getText());
            pst.setString(4, txtNetVal.getText());
            pst.executeUpdate();
        }catch(Exception e){JOptionPane.showMessageDialog(null, e);}

        idUpdater();
    }catch(Exception e){JOptionPane.showMessageDialog(null,""+ e);}

}                                        
于 2015-07-04T16:45:44.903 回答