2014-03-27 2 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#getResource 또는 Class#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 파일을 배포해야합니다. 기본 보고서의 경우에도 합병 과정이 짧지 않으므로 응용 프로그램의 성능이 향상됩니다. jar 파일의 lib 폴더에 이것은 당신이 사용하는 것이 의미

...

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

대신 JasperCompileManager.compileReport

1

설정 환경 변수

+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

자들과 같은 코드를 작성할 때마다 그것은 너무 늦기 경우 는 모르겠지만, 나는 항아리 실행 파일을 구축 한 후이 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*/ 
관련 문제