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:
Now the report can be created using a simple query, for instance
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.
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:
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:
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:
And I copied the following libs to it:
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.
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)
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