Jump to content

Run Jasper report and export as pdf from Java Swing application

Go to solution Solved by duka.milan,

Recommended Posts

I have made a Java Swing application which produces an xml file with data from a mysql database.

Using that I have successfully made a Jasper (jrxml) report using the Jasper 5.5.0 plugin for NetBeans separately from my Swing application.

To do this I use the prepared xml file as datasource. So I have Jasper report with an xml datasource and one subreport. How can I run that .jasper report from my Swing application and export as a pdf file? As a final message box from my Swing app I get:

"XML file is successfully generated"


I want to make one button on that message box with option:

"Print xml to pdf file"


How can I run my Jasper report and export as a pdf from Java class?


I have searched for examples using Google, but I can't find anything useful.

Link to comment
Share on other sites

  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

  • Solution

I found solution:

import net.sf.jasperreports.engine.JRException;

import net.sf.jasperreports.engine.JasperCompileManager;

import net.sf.jasperreports.engine.JasperExportManager;

import net.sf.jasperreports.engine.JasperFillManager;

import net.sf.jasperreports.engine.JasperPrint;

import net.sf.jasperreports.engine.JasperReport;

import net.sf.jasperreports.engine.JasperReportsContext;

import net.sf.jasperreports.engine.data.JRXmlDataSource;

public static void main(String[] args){

try { HashMap hm = new HashMap<>(); ,>

String filePath=System.getProperty("user.dir") + "\somexmldatasource.xml"; ,>

InputStream inputStream = new FileInputStream(new File(filePath)); ,>

JRXmlDataSource ds=new JRXmlDataSource(inputStream,"/some/xpath/query"); ,>

JasperReport jasperReport; ,>

JasperPrint jasperPrint; ,>

jasperReport = JasperCompileManager.compileReport(System.getProperty("user.dir")+"\yourreport.jrxml");,>

jasperPrint = JasperFillManager.fillReport(jasperReport, hm, ds); ,>

JasperExportManager.exportReportToPdfFile(jasperPrint, "someoutputpath\simple_report.pdf"); ,>

} catch (Exception e) { e.printStackTrace(); },>

} ,>

for using Jasper in java you must include several jars from c:Program Files (x86)JaspersoftiReport-5.5.0ireportmodulesext (or some other path of iReport app): commons-collections-3.2.1.jar, commons-digester-2.1.jar, commons-logging-1.1.jar, jasperreports-5.5.0.jar, xml-apis.jar, commons-beanutils-1.8.2.jar, servlet-api-2.4.jar and iText-2.1.7.js2.jar,>

Link to comment
Share on other sites

  • 1 month later...


Thanks for sharing the way out. I wonder whether the PDF processing tools can help with the related work or not? I am testing about the related projects these days.



Do you have any ideas about it? Or any good suggestion? Thanks in advance.




Best regards,



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...