Have you ever had a client that only wants a basic reporting screen that has the values in it? Good thing JasperReports was invented!
In this post, I’m going to share a small example of how you can create your own report in iReport, compile it to *.jasper and call them via your own Java Desktop App!
Step by Step
Create your report in iReport
In this example, let’s just create a simple Report (a blank perhaps) and put a few parameters and fields.
Once done, compile it and locate your jasper file (*.jasper is the compiled source file).
I wouldn’t dive much on how you where you should put your jasper files. It can be externally access or via resource classpath. My recommendation is of course, put it in your classpath, but we all have our own case of usage. If there will be a lot of adjustments to your report because of alignments, then I would go with putting it externally. This is so that you wouldn’t have to compile your project again just to do minor alignments.
Before we go to the snippet code, make sure you have the dependency below.
<dependency> <groupId>net.sf.jasperreports</groupId> <artifactId>jasperreports</artifactId> <version>5.5.0</version> </dependency>
If you’re not familiar with Maven, you should be, read here. 🙂
and here is a sample of code.
// Get the file, put it in a stream FileInputStream fis = new FileInputStream(this.getClass().getResource("").getFile()); BufferedInputStream bufferedInputStream = new BufferedInputStream(fis); // We do have parameters, so we create the map object to store values for them. Map map = new HashMap(); map.put("SAMPLE1", "This is a sample 1 text"); map.put("SAMPLE2", "This is a sample 2 text"); // We create the jasper report object, load the report with JasperFillManager object and use the // JasperViewer to display the report JasperReport jasperReport = (JasperReport) JRLoader.loadObject(bufferedInputStream); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, map,new JREmptyDataSource()); JasperViewer.viewReport(jasperPrint, false);
If you’re having issues, just let me know (via comments), I can help you out.