Reputation: 3270
I'm trying to get a JRPrint object so here's the code :
import java.io.File;
import net.sf.jasperreports.engine.*;
public class TestClass {
protected static JasperPrint jasperPrint;
static JasperReport jasperReport;
protected static JasperReportsContext jasperReportsContext;
public static void main(String[] args) {
String fileName = "/home/amira/Desktop/Map/testReports/test/textreport.jasper";
boolean isXMLFile = false;
if (!isXMLFile && fileName.endsWith(".jrxml"))
{
isXMLFile = true;
}
try
{
loadReportJrprint(fileName, isXMLFile, DefaultJasperReportsContext.getInstance());
}
catch (JRException e)
{
System.err.println("Error viewing report design."+ e);
System.exit(1);
}
try {
JasperExportManager.exportReportToPdfFile(fileName);
} catch (JRException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected static void loadReportJrprint(String fileName, boolean isXmlReport, JasperReportsContext jasperReportsContext) throws JRException
{
if (isXmlReport)
{
jasperPrint = JRPrintXmlLoader.loadFromFile(jasperReportsContext, fileName);
System.out.println(jasperPrint.getName());
}
else
{
jasperPrint = (JasperPrint)JRLoader.loadObjectFromFile(fileName);
}
}
}
But i'm getting this error :
Exception in thread "main" java.lang.ClassCastException: net.sf.jasperreports.engine.JasperReport cannot be cast to net.sf.jasperreports.engine.JasperPrint
at TestClass.loadReportJrprint(TestClass.java:80)
at TestClass.main(TestClass.java:50)
Upvotes: 0
Views: 7246
Reputation: 4166
A .jasper
file is mereley a compiled .jrxml
, so what you are retrieving is a JasperReport
object which describes a compiled template. To get a JasperPrint
object, which describes a document ready to be viewed or exported, you have to fill the report first with one of the JasperFillManager.fill
methods. See the javadoc for JasperFillManager.
Upvotes: 3