2014-10-12 20 views
-1

嗨朋友我已經創建了一個java項目,並且還使用該項目中的jasper報告生成報告,當我將我的java項目打包到.exe安裝程序時,jasper報告工作正常,但是當我在另一個系統中部署該exe安裝程序時, jasper報告不起作用ie:jasper報告在我的系統中調用,但不在另一個系統中,我知道我必須更改我的文件路徑,但我不知道如何提供在所有系統和Jasper中接受的文件路徑報告也將在另一個系統中正常工作。賈斯珀報告不工作在Java需要幫助來解決它?

我的代碼是:

 try(InputStream is = getClass().getResourceAsStream("C:\\Users\\Applebj\\Documents\\NetBeansProjects\\JavaApplication14\\src\\resources\\bil.jrxml")){ 
    String txt = jLabel1.getText(); 
    String t1=lb2.getText(); 
    String t2=lbl3.getText(); 
    Connection cn; 
    Class.forName("com.mysql.jdbc.Driver"); 
      cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/billing", "root", ""); 
    Map <String,Object> mp =new HashMap <String,Object>(); 
    mp.put("bj", txt); 
    mp.put("hpy",t1); 
    mp.put("li",t2); 

    // URL urpo=getClass().getResource("/resources/bil.jrxml"); 
    JasperReport jasperReport; 
    jasperReport = JasperCompileManager.compileReport(is); 
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,mp, cn); 
    // JasperExportManager.exportReportToPdfFile(jasperPrint, "C:\\Users\\Applebj\\Desktop\\rep\\simple_report.pdf"); 

    JasperExportManager.exportReportToPdfFile(jasperPrint, "bil.pdf"); 
    JasperViewer.viewReport(jasperPrint); 


} 
catch(Exception eey){ 
    eey.printStackTrace(); 
    System.out.println(eey.getMessage()); 
} 

好心幫我解決adavance這個問題感謝

+0

我猜測路徑爲'C:\\ Users \\ Applebj \\ Desktop \\ report \\ bil。 jrxml'在其他系統上找不到。您需要以某種方式將'.jrxml'與應用程序捆綁在一起... – MadProgrammer 2014-10-12 05:39:30

回答

1

取決於你如何建立你的應用程序,你需要確保.jrxml文件包括內產生.jar文件。

在Netbeans和Eclipse中,您應該能夠將.jrxml文件放入src目錄,並且它應該作爲構建過程的一部分捆綁在一起。

這意味着你將不再能指.jrxml爲一個文件,而是,你需要把它作爲一個嵌入式資源...

try (InputStream is = getClass().getResourceAsStream("/path/to/report/relative/to/the/src/directory/bil.jrxml")) { 
    JasperReport jasperReport = JasperCompileManager.compileReport(is); 
} 

(NB仍可能該JasperCompileManager將拋出你需要處理異常,但至少InputStream將正常關閉)

這句話的意思是,如果你把bil.jrxml報告中src/resources目錄(例如),你就需要使用路徑/resources/bil.jrxml

現在,我不打算在運行時編譯一個.jrxml文件,因爲它不是一個簡短的過程,報告過程可能已經花費幾秒鐘(如果不再更長),所以您不希望讓用戶等待,而是,預先編譯它們,並使用生成的.jasper文件代替,這可以在運行時加載...

+0

am得到以下錯誤,正如我所說的按照您的提交 找不到適用於compileReport(URL)的方法 方法JasperCompileManager.compileReport(String )不適用 (參數不匹配; URL不能轉換爲字符串) 方法JasperCompileManager.compileReport(InputStream)不適用 (參數不匹配; URL無法轉換爲InputStream) 方法JasperCompileManager.compileReport(JasperDesign)不適用 (參數不匹配; URL不能轉換爲JasperDesign) – user2563380 2014-10-12 06:51:31

+0

是的,這就是你沒有先諮詢API文檔。更新 – MadProgrammer 2014-10-12 07:51:01

+0

笏我必須更新?..告訴我清楚plzz – user2563380 2014-10-12 08:23:54