Cannot find Subreport from jar file - jasper-reports

I have completed my java project through Netbeans with Jasper Report. I copied the files to another laptop and run the jar file, but I get an error when i run the report from the jar file pertaining to the subreport:
net.sf.jasperreports.engine.jrexception resource not found at ...path
It tried to look on the original path of the subreport where I have developed my program and reports. Now, since I want to run the program to another laptop, I dont have that path anymore. What to do? How to set the subreports to a relative path? I have this subreport expression:
$P{SUBREPORT_DIR} + "SubReport_Items.jasper"
Please help...

You can not use relative path in jasper report, that's why you normally use $P{SUBREPORT_DIR}, since the $P{SUBREPORT_DIR} is parameter, your application can use relative path and then pass the absolute path to jasper report es.
java code example
File reportDir = new File("myReportDirRealtiveToApplication"); //relative path
String absolutePath = reportDir.getAbsolutePath() + File.separator; //Convert to absolute
Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put("SUBREPORT_DIR", absolutePath); //Pass as parameter
pass the parameter map to your export/fill method, below is an example, naturally you will use what ever you are using currently....
JasperFillManager.fillReportToFile(jasperFileName, paramMap, myConnection); //Fill the report

Related

How to set Jasper to use XSSF?

In a existing system already use jasper 5.0 and as I know it use poi HSSF to generate xls data, but now as the application grow up, the report have a problem with a big count transaction to generate.
I have search for the solution and found POI with XSSF. Because jasper use POI HSSF too, I thinking about to use XSSF inside of JASPER.
Is that possible? and how I could do that? I need to use jasper because it's not possible for now to change the existing app.
To export jrxml generating ooxml XSSF, excel file xlxs
Use the net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter
Example:
JasperPrint jasperPrint = JasperFillManager.fillReport(report, paramMap, connection); //Example of how to get the jasper print
JRXlsxExporter exporter = new JRXlsxExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
File outputFile = new File("excelTest.xlsx");
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputFile));
SimpleXlsxReportConfiguration configuration = new SimpleXlsxReportConfiguration();
configuration.setOnePagePerSheet(false); //Set configuration as you like it!!
configuration.setDetectCellType(true);
configuration.setCollapseRowSpan(false);
exporter.setConfiguration(configuration);
exporter.exportReport();
Naturally you need related libraries (poi-ooxml.jar, poi-ooxml-schemas.jar, xmlbeans.jar) in your classpath, they are present in the distribution of jasper report.
The JRXlsxExporter is available since version 4.5 this is the jasper report 5.5.0 API. In version 4 parameters where set instead of properties please see jasperreports-export-to-xlsx-not-xls

Why is iReport throwing me these subreport exceptions?

I am new to iReport and all I have done so far is simply run an already compiled report. In fact it's a report that we are currently using so there should not be any issue. Though, when ever I run the report I get thrown this error message
 Compiling to file... C:\Program Files\Fishbowl\reports\Custom\PackingListTacticalMedical2015.jasper
 Compiling subreports....
 Subreport C:\Program Files\Fishbowl\reports\Custom\SOPackingListTacticalMedical2015.jrxml already compiled.
 **Unable to locate the subreport with expression: "$P{cbLayoutFormat}.equals("standard") ? $P{path} + "POPackingList.jasper" : $P{cbLayoutFormat}.equals("carton") ? $P{path} + "cartonPOPackingList.jasper" : $P{path} + "POPackingListLoc.jasper"".**
Compilation running time: 13,619
The report does still run and compile but when ever I am prompted to "Enter a shipID" - and I do- the results are blank. I know what the message is saying, I just simply do not understand what it means.
Thanks to anyone that can help with instructions or explanations on this.
Try to change expression datatype of your subreports to InputStream instead of String (path), and then passe your subreport as input stream as a parameter.
Put your subreport in the resources dir. and compile it.
InputStream subReportStream=getClass().getResourceAsStream("subReportName.jasper");
params.put("SUBREPORT_STREAM", subReportStream);
this work fine for me.
With the way Fishbowl inserts it's run path into the $P{path} parameter you need turn off the compile subreports option when compiling and making changes to the the report. Also with the manner in which Fishbowl overrides the parameters at runtime you'd have to know all of the database ID's you're looking for if you want to run it from within iReport instead of inside Fishbowl.
The easiest work around for this is to have the reports compile to the their working directory and you can refresh inside Fishbowl as you compile changes to the reports.

How do I copy the TestNG output folder to some other folder?

I do have the code to do it, but when do I do it? It seems like when I do it AfterSuite I am copying the previous test-output files. I also created a custom listener but that didn't solve the problem since the report is created at the very end.
If this is not possible, is it then possible to programmatically change the output directory?
One way can be if you run testng programatically. You get control to set the output directory.
TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
testng.setOutputDirectory("My dir");
testng.setTestClasses(new Class[] { TestClass1.class });
testng.addListener(tla);
testng.run();
The other question of moving all default reports, you can have a reporter which moves the file, but since the order of reporters is not gauranteed, you can't guarantee that your reporter runs last.

Replacing aspose JRPptExporterParameter.PPT_TEMPLATE_PRESENTATION in jasperreports 5.0

I am generating a ppt report using jasperreports and aspose library (ppt exporter from aspose). I'm trying to eliminate aspose from the project and use the ppt exporter from jasperreports 5.0. The problem is that at the moment the generated report needs an external .pot file which is added using aspose:
com.aspose.slides.jasperreports.JRPptExporter exporter = new com.aspose.slides.jasperreports.JRPptExporter();
......
exporter.setParameter(com.aspose.slides.jasperreports.JRPptExporterParameter.PPT_TEMPLATE_PRESENTATION, pptTemplate);
exporter.exportReport();
I didn't find any similar parameter in JRExporterParameter from jasperreports and I couldn't find any efficient solution yet. Is there any method of using an external .pot file? I was thinking about creating a second JasperPrint object from the .pot file and then exporting both JasperPrint objects setting JRExporterParameter.JASPER_PRINT_LIST
Not sure if that fits for you, but I've written a custom PPTX Exporter (only pptx, not binary ppt), which is based on Apache POI. The POI element can be initalized by your own template pptx (not yet implemented in my version).
https://code.google.com/p/pptx-shape-exporter/
Drop me a line, if this sounds interesting to you.

Exporting an JAR file in Eclipse and referencing a file

I have a project with a image stored as a logo that I wish to use.
URL logoPath = new MainApplication().getClass().getClassLoader().getResource("img/logo.jpg");
Using that method I get the URL for the file and convert it to string. I then have to substring that by 5 to get rid of this output "file:/C:/Users/Stephen/git/ILLA/PoC/bin/img/logo.jpg"
However when I export this as a jar and run it I run into trouble. The URL now reads /ILLA.jar!/ and my image is just blank. I have a gut feeling that it's tripping me up so how do I fix this?
Cheers
You are almost there.
Images in a jar are treated as resources. You need to refer to them using the classpath
Just use getClass().getResource: something like:
getClass().getResource("/images/logo.jpg"));
where "images" is a package inside the jar file, with the path as above
see the leading / in the call - this will help accessing the path correctly (using absolute instead of relative). Just make sure the path is correct
Also see:
How to includes all images in jar file using eclipse
See here: Create a file object from a resource path to an image in a jar file
String imgName = "/resources/images/image.jpg";
InputStream in = getClass().getResourceAsStream(imgName);
ImageIcon img = new ImageIcon(ImageIO.read(in));
Note it looks like you need to use a stream for a resource inside an archive.