Run Jasper report and export as pdf from Java Swing application

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.
duka.milan's picture
Joined: Dec 17 2013 - 1:54pm
Last seen: 7 years 3 months ago

1 Answer:


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;


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)\Jaspersoft\iReport-5.5.0\ireport\modules\ext\ (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,>

duka.milan's picture
Joined: Dec 17 2013 - 1:54pm
Last seen: 7 years 3 months ago

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,

arronwall1 - 7 years 2 months ago