它在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);
}
}
提前感谢!
我的图书馆是:
答案 0 :(得分: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
。
答案 1 :(得分: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);}
}
答案 2 :(得分:0)
在jar文件中使用已编译的jasper报告效率不高。我建议您从PC上的某个位置(例如C:驱动器)引用该报告。
如果您有一个易于访问的永久文件夹,那也将是最好的。可能是C:/<project name>/report/<your-report>
。
这种方法使您可以轻松分发它。