Monday, September 12, 2011

Compile Basic JasperReports in Java

I used to compile JasperReports in iReport and deploy the .jasper file to production, instead of .jrxml file.

Recently my company request me to change to compilation from iReport to java code. I not sure why they want to change. The changes should be very simple and easy but it took me few days to find out how to compile JasperReports in java.

See below sample code to compile .jrxml file in java. Remember to add iText, JasperReports and jdt-compiler jar files into classpath. Without jdt-compiler jar file you can't compile.

public class BasicJasperReportTest {

public static void main(String[] args) {

try {

JasperDesign jasperDesign = JRXmlLoader.load("C:/report1.jrxml");

JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap(), new JREmptyDataSource());

JasperExportManager.exportReportToPdfFile(jasperPrint,"C:/report1.pdf");

JasperViewer.viewReport(jasperPrint);

} catch (JRException e) {

e.printStackTrace();

}

}

}

1 comment:

Anonymous said...

final soccer vr slot - Shootercasino.com 188bet 188bet 다파벳 다파벳 クイーンカジノ クイーンカジノ 329Mohegan Sun Winter Garage | Riders Casino