gpt4 book ai didi

java - 执行 jar 文件时没有显示 jasper 报告

转载 作者:行者123 更新时间:2023-11-29 08:59:56 28 4
gpt4 key购买 nike

当我在 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
  • commons-logging-1.1
  • jasperreports-3.0.1
  • jdt-compiler-3.1.1
  • jfreechart-1.0.3
  • poi-3.0.1-FINAL-20070705

最佳答案

问题是您没有将 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);
}

此外,在将项目导出到 .jar 之前,将 .jrxml 文件添加到项目的某个包中。

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

关于java - 执行 jar 文件时没有显示 jasper 报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18069817/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com