How to load .jasper instead of .jrxml

0

Hi,

Right now I'm compiling and loading my jasper report from .jrxml file as following? How do I load my reports from a pre-compiled .jasper file?

 

Code:
            JasperDesign jasDesign = JRXmlLoader.load("D:/Working/MyApp/src/MyoApp/MyReportsJrxml/AccountActivity.jrxml");
            JasperReport jasReport = JasperCompileManager.compileReport(jasDesign);
            JasperPrint jasPrint = JasperFillManager.fillReport(jasReport, parameters, con);
            JasperViewer.viewReport(jasPrint,false);



Also, How do I use this absolute path with a reletive path?

Appreciated....



Post Edited by Nasir Amir at 04/02/09 18:38
nkamir's picture
12
Joined: Jan 29 2009 - 4:21pm
Last seen: 10 years 1 month ago

thanks

 

onlyappasaheb4 - 2 years 7 months ago

6 Answers:

1

You should use JRLoader.loadObject to load compiled reports.

If you use relative paths they would be interpreted as relative to the current user directory, as per standard Java rules (see java.io.File).

HTH,

Lucian

Code:
JasperReport jasReport = (JasperReport) JRLoader.loadObject(compiledReportFile);</td></tr></tbody></table>
lucianc's picture
7045
Joined: Jul 17 2006 - 1:10am
Last seen: 1 day 3 hours ago

do you have an updated example of this that does not use deprecated APIs?

arch_1 - 6 years 3 weeks ago

Actually found it

arch_1 - 6 years 3 weeks ago
0

Nice, the loadObject works just fine, Thanks.

Now, that document says:
"A relative pathname that does not specify a drive has no prefix."

but I get FileNotFoundException while trying it as following:
 
the absolute path of the jasper object is:
D:/Working/MyApp/src/MyApp/MyReportsJrxml/AccountActivityReport.jasper

the absolute path of the java class  is:
D:/Working/MyApp/src/MyApp/MyReports/AccountActivityReport.class

I'm trying to access it as
../MyReportsJrxml/AccountActivity.jasper";
 

what could be the issue?

nkamir's picture
12
Joined: Jan 29 2009 - 4:21pm
Last seen: 10 years 1 month ago
0

What you want to export file type?

Html?xls?pdf?

This is my test code,export fill type is html,and it run successful.

  String reportFilePath = root_path
    + "WEB-INF/classes/com/sunshine/jasper/report.jasper";

JasperReport report = (JasperReport) JRLoader.loadObject(reportFilePath);
   JasperPrint jasperPrint = JasperFillManager.fillReport(report,null, conn);
      
   JRHtmlExporter exporter = new JRHtmlExporter();
   req.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE,jasperPrint); 
   exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI,"image?image=");   
   exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
   exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
   exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "GBK");
   exporter.exportReport();

 

Hope help you.

nopain_nogain's picture
Joined: Mar 24 2009 - 8:59pm
Last seen: 9 years 12 months ago
0

 
Actually, I'm able to generate my reports with absolute path, but I can't figure out how to use relative path to do the same thing.
 

The class that is calling the report file is:
D:/MyApplication/src/App/MyReports/AccountActivityReport.class

and the report file is:
D:/MyApplication/src/App/MyReportsJrxml/AccountActivityReport.jasper

  
I wanna call this report as:
../MyReportsJrxml/AccountActivity.jasper"
 
 
Appreciated...
 
 
 

 



Post Edited by Nasir Amir at 04/06/09 13:13
nkamir's picture
12
Joined: Jan 29 2009 - 4:21pm
Last seen: 10 years 1 month ago
0

Again, relative paths are relative to the JVM's current user directory, and not to the class that calls JasperReports methods.

If you want to specify paths as relative to your class, you'll have to call Class.getResource() yourselft and pass the result URL to JasperReports.

Regards,

Lucian

lucianc's picture
7045
Joined: Jul 17 2006 - 1:10am
Last seen: 1 day 3 hours ago
1

I use InputStreams to do the load that way I can have it in either class path or physical file or some other stream source.

InputStream jasperStream = getClass().getResourceAsStream("/Foo.jasper");
JasperReport report = (JasperReport) JRLoader.loadObject(jasperStream);

arch_1's picture
12
Joined: Feb 25 2013 - 3:18pm
Last seen: 6 years 3 weeks ago
Feedback