2014-03-27 34 views
-1

我已經編寫了下面的代碼來創建jasper報告,這段代碼在NetBeans IDE中正常工作,但是在創建該項目的jar文件之後,報告未打開。它也沒有顯示任何錯誤。製作jar文件後Jasper報告不工作

可能是什麼問題?

代碼創建碧玉報告

//Path to your .jasper file in your package 
    String reportSource = "src/report/Allvendor_personal_info.jrxml"; 
    try 
    { 
    jasperReport = (JasperReport) 
JasperCompileManager.compileReport(reportSource); 
    jasperPrint = JasperFillManager.fillReport(jasperReport, null, con); 

    //view report to UI 
    JasperViewer.viewReport(jasperPrint, false); 
    con.close(); 
    } 
catch(Exception e) 
    { 
    JOptionPane.showMessaxgeDialog(null, "Error in genrating report"); 
    } 
+3

路徑'src'不會在運行時存在,你永遠不應該引用它 – MadProgrammer

回答

2

路徑src不會在運行時存在,你永遠不應該引用它。

在此基礎上,該Allvendor_personal_info.jrxml將是一個嵌入的資源,存儲在JAR文件中,你將不能夠像你一樣普通文件來訪問它,相反,你需要使用Class#getResourceClass#getResourceAsStream

String reportSource = "/report/Allvendor_personal_info.jrxml"; 
InputStream is = null; 
try 
{ 
    is = getClass().getResourceAsStream(reportSource); 
    jasperReport = (JasperReport)JasperCompileManager.compileReport(is); 
    jasperPrint = JasperFillManager.fillReport(jasperReport, null, con); 
//... 
} finally { 
    try { 
     is.close(); 
    } catch (Exception exp) { 
    } 
} 

現在,說了這麼多,在運行時應該沒有什麼理由要編譯.jrxml文件,相反,您應該在編譯時編譯這些文件並改爲部署.jasper文件。這樣可以提高應用程序的性能,因爲即使是基本報告,併發程序也不短。

這意味着你可以使用...

jasperReport = (JasperReport) JRLoader.loadObjectFromFile(is); 

而不是JasperCompileManager.compileReport

1

設置環境變量的jar文件lib文件夾

+0

能否請你告訴我那是什麼?我得到同樣的問題。 http://stackoverflow.com/questions/25842748/unable-to-call-jasper-report-from-jar-file/25842817?noredirect=1#comment40436818_25842817 –

0

你需要複製它有碧玉/ JRXML文件夾文件並將其放在jar文件的相同目錄中。 每當你寫這樣

String reportSource = "/report/Allvendor_personal_info.jrxml"; 
//It will look for this file on your location so you need to copy your file on /report/ this location 
InputStream is = null; 
try 
{ 
is = getClass().getResourceAsStream(reportSource); 
jasperReport = (JasperReport)JasperCompileManager.compileReport(is); 
jasperPrint = JasperFillManager.fillReport(jasperReport, null, con); 
//... 
} catch(Exception e){ 

} 
1

好球員的代碼,我不知道這是爲時已晚,但我浪費了1天建立一個可執行的Jar後搜索解決這個問題,關於JasperReport的。爲了讓您的報告建立一個罐子後的工作,你只需要編寫以下行

String reportUrl = "/reports/billCopyReport.jasper"; //path of your report source. 
InputStream reportFile = null; 
reportFile = getClass().getResourceAsStream(reportUrl); 

Map data = new HashMap(); //In case your report need predefined parameters you'll need to fill this Map 

JasperPrint print = JasperFillManager.fillReport(reportFile, data, conection); 
JasperViewer Jviewer = new JasperViewer(print, false); 
Jviewer.setVisible(true); 

/* var conection is a Connection type to let JasperReport connecto to Database, in case you won't use DataBase as DataSource, you should create a EmptyDataSource var*/