Skip to main content

ADF 12c : Using Jasper Reports en JasperSoft Studio 6.1; What Libraries do you need?

Over the last couple of years, or better in the last decade I have implemented several reporting solutions with Jasper Reports in ADF. I did that in ADF 10g, ADF 11.1.1.x, ADF 11.1.2.x and ADF 12.1.x
I also used several version of Jasper Reports. There is a whole lot of documentation, blogposts and presentations available. So when today I got a request from one of my customers to make a setup for the implementation of Jasper Reports 6.1 in ADF 12.1.3 I did not expect any problems. Boy was I wrong.

Here is the Story
With all the knowledge from the past, I decided to follow the known steps.
1) Download iReport Designer,
2) Build a report in iReport
3) Create an ADF application
4) Add the necessary libraries to use the report
5) Call the report from a button via a Managed Bean

Step 1
In the past I used iReport designer to build the reports. When you go to the download site of iReport designer you now see an interesting message.


So I took this serious and decided not to use iReport Designer, but to use JasperSoft Studio. It proves to work pretty much the same, so I was able to create a simple sample report.

Step 2
The simple sample report uses a Data Adapter to an Oracle Database (HR) that can be created as in step 5 of this tutorial. Take special note of the classpath of Database Library Driver. I use the one that is shipped with JDeveloper. That one can be found in:
1:  <jdeveloperhome>/oracle_common/rda/da/lib/ojdbc14.jar  

Now the report can be created using a simple query, for instance

 select * from EMPLOYEES  
Or you can use this tutorial to build your own report.

Step 3 and 4 and 5
In the new ADF Application we need to make sure to add the jasperReports Library to your viewController project. I used jasperreports-6.1.0.jar

With that in place I can now use my code from the past that needs to be in the backing bean to call the report from a button. That code should still work and is exactly as I show here.
1:  public class ReportingBean {  
2:    public ReportingBean() {  
3:    }  
4:    public void startReport(ActionEvent actionEvent) {  
5:      // Add event code here...  
6:      String filepath = "C:/JDeveloper/mywork/reports/MyReports/";  
7:      String reportname = "dummy.txt";  
8:      InputStream is;  
9:      try {  
10:        OutputStream os = new FileOutputStream(new File(filepath + reportname));  
11:        Map parameters = new HashMap();  
12:        // parameters.put("P_DEPARTMENT_ID", getDepartmentId());  
13:        Connection conn = getConnectionDS("java:comp/env/jdbc/MYHRDS");  
14:        JasperReport jasperReport;  
15:        //Note; we have two options. EIther we use the .jasper file and run the compiled report  
16:        //    or we use the .jrxml file that needs to be compiled at runtime.  
17:        if(runNoComplile){       
18:        // from .jasper file, so without compiling        
19:         InputStream jasperStream = new FileInputStream(new     
20:                 File("C:/JDeveloper/mywork/reports/MyReports/EMP_A4.jasper"));  
21:         jasperReport=   (JasperReport) JRLoader.loadObject(jasperStream);  
22:        // end from .jasper  
23:        }  
24:        else{  
25:        // from jrxml, so we do a runtime compile  
26:         is = new FileInputStream(new  
27:             File("C:/JDeveloper/mywork/reports/MyReports/EMP_A4.jrxml"));  
28:         JasperDesign jasperDesign = JRXmlLoader.load(is);  
29:         jasperReport = JasperCompileManager.compileReport(jasperDesign);  
30:       // end from jrxml  
31:        }  
32:        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, conn);  
33:        JasperExportManager.exportReportToPdfStream(jasperPrint, os);  
34:        JasperViewer.viewReport(jasperPrint, false);  
35:      } catch (FileNotFoundException e) {  
36:        e.printStackTrace();  
37:      } catch (JRException e) {  
38:        e.printStackTrace();  
39:      } catch (NamingException e) {  
40:        e.printStackTrace();  
41:      } catch (SQLException e) {  
42:        e.printStackTrace();  
43:      }  
44:    }  
45:    public static Connection getConnectionDS(String dsName) throws NamingException, SQLException {  
46:      Connection connection = null;  
47:      try {  
48:       javax.naming.Context initialContext = new javax.naming.InitialContext();  
49:       javax.sql.DataSource dataSource =   
50:            (javax.sql.DataSource)initialContext.lookup(dsName);  
51:       connection = dataSource.getConnection();  
52:       } catch(Exception e){  
53:         e.printStackTrace();  
54:         //or handle more gracefully   
55:       }  
56:      return connection;  
57:    }  
58:  }  

The necessary imports are added to the class from the jasperreports-6.1.0.jar library, the class compiles, the project builds and the application runs. However, at runtime, you will probably run into trouble. You will see several ClassNotFound Exceptions. At least I did run into these. It took me a couple of hours before I was able to fix these issues. It all had to with the library dependencies in the ViewController project.

So what exactly should you add, and where can you find it
All that the documentation tells you is that you need to add the jasperreports-6.1.0.jar library, however there is more that should be added to make this all work.
From the past I know that we should add the following:


So I decided to add whatever I could find that resembles those libs that made my reports work in the past. In the iReport solution these libraries were located in:
1:  <reportshome>\iReport-4.0.1\ireport\modules\ext  

In the new situation, with JasperSoft Studio, I was unable to find these libraries (or the new versions of them) in the reportshome location. So where have they gone?

I finally managed to find them in the download of jasperReports-6.1.0-project.zip. If you download the file and unzip it you will find all necessary libraries in:
1:  jasperreports-6.1.0/lib  

You can now copy the libraries from there to a folder that you use to bundle those libraries into your ADF Project. So what I did was the following: I created a folder called:
1:  /extralibs/jasper  

And I copied the following libs to it:
  • groovy-all-2.0.1.jar
  • iText-2.1.7.js2.jar
  • jasperreports-6.1.0.jar
  • poi-3.10.1.jar
  • jfreechart-1.0.12.jar
  • jcommon-1.0.15.jar
  • ant-1.7.1.jar (not sure if I need this one)
This resembles pretty much what I used to have in previous versions of my reports implementation. I was convinced that it would work now, so I gave it a try. Almost everything worked...

Except for the report that needs to be compiled (line 29 in the code sample above). Too bad, compilation failed and I was almost giving up. After a lot of googling and trying, I finally found a solution: Apparently Jasper reports uses the ecj library to do the compilation of reports. This library happens to be available from the same location as the ones that I added previously. Once I added this library to my project, all worked perfectly. The image below shows what libs were added by me in order to make it work.


Final note
I added the libraries to the ViewController project via the Jdeveloper Library Dependencies. You can (or should) use Maven or ANT to manage the library dependencies, as this is much more flexible.

Comments

Unknown said…
Hi, Luc and thank you for sharing your knowledge. A couple of months ago I also needed to demonstrate the power of Jasper Reports integrated in an ADF application. You can check my solution at http://blog.dreamix.eu/oracle-2/integrating-jasperreports-in-adf-application . If you need a code sample of how to create dynamically ADF input controls depending on the report's parameters included in the SQL query, feel free to reuse or modify my code described in the blog post. Keep up with spreading good practices and skills!
Unknown said…
thanks too much handy article

Popular posts from this blog

ADF 12.1.3 : Implementing Default Table Filter Values

In one of my projects I ran into a requirement where the end user needs to be presented with default values in the table filters. This sounds like it is a common requirement, which is easy to implement. However it proved to be not so common, as it is not in the documentation nor are there any Blogpost to be found that talk about this feature. In this blogpost I describe how to implement this. The Use Case Explained Users of the application would typically enter today's date in a table filter in order to get all data that is valid for today. They do this each and every time. In order to facilitate them I want to have the table filter pre-filled with today's date (at the moment of writing July 31st 2015). So whenever the page is displayed, it should display 'today' in the table filter and execute the query accordingly. The problem is to get the value in the filter without the user typing it. Lets first take a look at how the ADF Search and Filters are implemented by...

ADF 11g Quicky 3 : Adding Error, Info and Warning messages

How can we add a message programatically ? Last week I got this question for the second time in a months time. I decided to write a short blogpost on how this works. Adding messages is very easy, you just need to know how it works. You can add a message to your faces context by creating a new FacesMessage. Set the severity (ERROR, WARNING, INFO or FATAL ), set the message text, and if nessecary a message detail. The fragment below shows the code for an ERROR message. 1: public void setMessagesErr(ActionEvent actionEvent) { 2: String msg = "This is a message"; 3: AdfFacesContext adfFacesContext = null; 4: adfFacesContext = AdfFacesContext.getCurrentInstance(); 5: FacesContext ctx = FacesContext.getCurrentInstance(); 6: FacesMessage fm = 7: new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, ""); 8: ctx.addMessage(null, fm); 9: } I created a simple page with a couple of buttons to show the result of setting the message. When the but...