Jump to content
We've recently updated our Privacy Statement, available here ×

How to execute Jasper Report through Java?


Ankur Gupta
Go to solution Solved by Ankur Gupta,

Recommended Posts

Hi, 
Can anyone please tell me a way to execute the jasper report through java , as well as please tell me which jars are needed to be used?

Also please tell hoe to pass prameters and as well as how to retrieve which parameters are required through java program ,in order to run jasper report.

 

Thanks,

Ankur

Link to comment
Share on other sites

  • Replies 6
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Hi, 

The code depends on the datasource you want to use for generating the report, this one is an example that shows how to create a pdf file having a xml datasource and a jrxml file: 

 

      File f = new File ("XmlDataSource.xml");
      JRXmlDataSource JprXmlDsr = new JRXmlDataSource (f, "DataList/Line");
      
        /* Compile the template */
      JasperReport Rep = JasperCompileManager.compileReport ("MyReport.jrxml");
 
        /* Create the JasperPrint object with the template and the data */
      JasperPrint  Prn = JasperFillManager.fillReport (Rep, new HashMap (), JprXmlDsr);
      
        /* Export the report to pdf format if needed*/
      JasperExportManager.exportReportToPdfFile (Prn, "MyReport.pdf");
 
About the jar files you need, there is a post that tells the necessary ones : http://community.jaspersoft.com/questions/529175/required-jar-files-compilingfillingexporting
 
About passing parameters to the report, you can see that in the code I have written above there is a HashMap in the call to "fillReport" function. You can add all the parameters you need in this HashMap. For example: 
 
HashMap map = new HashMap ();
HashMap.put ("ParamName", paramValue);
 
I'm not sure if it is possible to do the las thing you want to do. If I'm not wrong, you want to know how many parameters do you have in your jrmxl report. The only way I know would be parsing the jrxml file but don't know if there is any other option. 
 
Hope this helps.
 
Regards
     
Link to comment
Share on other sites

In thas case you need to get the connection to the database first. This link can help you: http://www.roseindia.net/tutorial/java/jdbc/jdbcdrivermanager.html

Then, the code should be very similar to the one I have posted. The only modification is the call to fillReport function: 

JasperPrint  Prn = JasperFillManager.fillReport (Rep, new HashMap (),  Con);

As you can see the only modification is in the last parameter "Con" (java.sql.Conneciton type), which is the connection to your database, and you can get it in the link I have put above. 

Hope this helps.

Regards

Link to comment
Share on other sites

Have followed the steps but I am getting a exception:

 

Exception in thread "main" java.lang.NoClassDefFoundError: org/codehaus/groovy/control/CompilationFailedException

at java.lang.Class.forName0(Native Method)

at java.lang.Class.forName(Class.java:247)

at net.sf.jasperreports.engine.util.JRClassLoader.loadClassForRealName(JRClassLoader.java:157)

at net.sf.jasperreports.engine.util.JRClassLoader.loadClassForName(JRClassLoader.java:115)

at net.sf.jasperreports.engine.JasperCompileManager.getCompiler(JasperCompileManager.java:730)

at net.sf.jasperreports.engine.JasperCompileManager.compile(JasperCompileManager.java:240)

at net.sf.jasperreports.engine.JasperCompileManager.compile(JasperCompileManager.java:173)

at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:448)

at Report.main(Report.java:43)

Caused by: java.lang.ClassNotFoundException: org.codehaus.groovy.control.CompilationFailedException

at java.net.URLClassLoader$1.run(URLClassLoader.java:200)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(URLClassLoader.java:188)

at java.lang.ClassLoader.loadClass(ClassLoader.java:303)

at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)

at java.lang.ClassLoader.loadClass(ClassLoader.java:248)

at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:316)

 

Can you tell where is the problem

 

Regards,

Ankur

Link to comment
Share on other sites

Hi, 

In the report Properties there is an option "Language" (check the attachment). I think your poblem is that you have selected "Groovy" instead of Java. Try changing this property and setting it as Java. 

This might solve your problem.

Regards

Link to comment
Share on other sites

  • Solution

Hi all ,

I have found a solution to the query on my own, thanks augarte for the explanation regarding the implementation in java that helped,but the real problem was that retrieving the parameters from the jrxml .It is possible to do so.

I found a way for that by parsing the xml and then extracting the tags with parameter and retrieving the attribute name and class keeping in mind the check attribut  isForPrompting is blank or not present for the parameters , In this way I got the Hashmap of all the parameters with their types and I put that in the parameterMap and hence fulfilling the parameters in the jrxml dynamically .Solves the purpose for me.

The jars need to be included are-

1.commons-beanutils-x.x.x.jar

2.commons-collections-x.x.x.jar

3.commons-digester-x.x.jar

4.commons-logging-x.x.jar

5.groovy-all-x.x.x.jar

6.iText-x.x.x.jar

7.jasperreports-x.x.x.jar

8.poi-x.x.jar(for excel export)

9.jfreechart-x.x.x.jar

Hope that this is helpful

Regards,

Ankur

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...