Spring Webflux: FileNotFoundException inside my JAR - java

I am exports PDF usgin jasper reports. In develop, it works fine. But when compile the jar, system throws and FileNotFound Exception for "src/main/resources/reports/myJasperReport.jxml"
When I explore the JAR,I came across that the URL for the report is "/BOOT-INF/classes/resports/myJasperReport.jxml"
I found this link to File inside jar is not visible but didn't solve my problem.
Could you help me please?
#Slf4j
#Service
public class ReportService {
private static final String REPORTS_BASE_PATH = "src/main/resources/reports/";
public ByteArrayInputStream exportReport(
String reportFileName,
Integer idCC,
Map<String,Object> parameters
) throws Exception {
Connection connection = CustomEntityManagerFactoryPostgresImpl
.getInstance()
.getConnection(idCC);
File file = ResourceUtils.getFile(REPORTS_BASE_PATH + reportFileName);
JasperReport jasperReport = JasperCompileManager.compileReport(file.getAbsolutePath());
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
return new ByteArrayInputStream(outputStream.toByteArray());
}
}

Add classpath: prefix to your path. And consider the resources folder to be a root.
ResourceUtils.getFile("classpath:reports/" + reportFileName);
UPD:
Actually, you don't need to get a file at all. Try to get resource as stream:
InputStream report = ReportService.class.getClassLoader().getResourceAsStream("reports/" + reportFileName);
JasperReport jasperReport = JasperCompileManager.compileReport(report);

Related

JasperReport File Not Found error in fat jar

I made a fat jar that contains JasperReport but when I try to execute the jar this pop's out in the cmd
net.sf.jasperreports.engine.JRException: java.io.FileNotFoundException: file:\C:\Users\User\Desktop\test.jar!\fxml\Report.jrxml (The filename, directory name, or volume label syntax is incorrect)
the "!" makes the problem here I think, but I don't know why it is added to the path nor how to remove it.
Here is my code:
public class PrintReport extends JFrame {
public void showReport(Connection conn) throws JRException {
String reportSrcFile = getClass().getResource("/fxml/Report.jrxml").getFile();
String reportsDir = getClass().getResource("/fxml/").getFile();
JasperReport jasperReport = JasperCompileManager.compileReport(reportSrcFile);
// Fields for resources path
HashMap<String, Object> parameters = new HashMap<String, Object>();
parameters.put("reportsDirPath", reportsDir);
ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
list.add(parameters);
JasperPrint print = JasperFillManager.fillReport(jasperReport, parameters, conn);
JRViewer viewer = new JRViewer(print);
viewer.setOpaque(true);
viewer.setVisible(true);
this.add(viewer);
this.setSize(1000, 500);
this.setVisible(true);
}}
Resources are not accessible as File once you package them in a jar. Load the report from an InputStream instead of a file instead:
JasperReport jasperReport = JasperCompileManager.compileReport(getClass().getResourceAsStream("/fxml/Report.jrxml"));
As for the parameter: Try using resource paths, if it's used to resolve resources in your report file. JasperReports should be able to access resources via ClassLoader.

I can not choose where to save the generated report in Jasper

I have a problem when I click to generate a report ... I would like the moment I clicked the button to generate the report to be shown a window asking where I want to save the document, the way it is now I'm in java code specifying the location and file name, and always the file is saved in the specified place in the code, I do not want it, I need to leave it open for the person to choose where to save ... down goes a piece of code I am using.
.
try {
URL arquivo = getClass().getResource(/reports/term.jasper);
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(arquivo);
//It generates the dto that will be sent to IReport
ArrayList<MinutoTRDto> dataList = getDataBeanList(licitacao);
JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(dataList);
Map<String, Object> parameters = getParametros();
JasperPrint print = JasperFillManager.fillReport(jasperReport, parameters, beanColDataSource);
JROdtExporter exporterOdt = new JROdtExporter();
exporterOdt.setExporterInput(new SimpleExporterInput(print));
// HERE IS THE PROBLEM!
exporterOdt.setExporterOutput(new SimpleOutputStreamExporterOutput("C://teste//sample_report.odt"));
exporterOdt.exportReport();
} catch (JRException jre) {
jre.printStackTrace();
}
You will have to write the following in your code
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition",
"attachment;filename=<your file name>");
With this, the 'file download' dialog will appear. You can refer here for the complete code.
Following the AMDG replied, I changed my file to the following code, only it was launched the following exception:
org.apache.jasper.JasperException: java.lang.IllegalStateException: getOutputStream() has already been called for this response
I using Wicket from framework
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
URL arquivo = getClass().getResource(REPORT_PATH);
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(arquivo);
ArrayList<MinutoTRDto> dataList = getDataBeanList(licitacao);
JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(dataList);
Map<String, Object> parameters = getParametros();
print = JasperFillManager.fillReport(jasperReport, parameters, beanColDataSource);
JROdtExporter exporterOdt = new JROdtExporter();
exporterOdt.setExporterInput(new SimpleExporterInput(print));
exporterOdt.setExporterOutput(new SimpleOutputStreamExporterOutput(outputStream));
exporterOdt.exportReport();
byte[] bytes = outputStream.toByteArray();
if (bytes != null && bytes.length > 0) {
HttpServletResponse response =(HttpServletResponse)((WebResponse)comp.getResponse()).getContainerResponse();
response.setContentType("application/odt");
response.setHeader("Content-disposition", "inline; filename=\"file_generated.odt\"");
response.setContentLength(bytes.length);
ServletOutputStream outputStream2 = response.getOutputStream();
outputStream2.write(bytes, 0, bytes.length);
outputStream2.flush();
outputStream2.close();
}
} catch (Exception e) {
e.printStackTrace();
}

My Jasper file wont work

I'm trying to make this program but i cannot take a report out of it... even if it worked in java netbeans when i tried to run it using the executable JAR file i get this error !
my coding part is
JRTableModelDataSource datasource = new JRTableModelDataSource(dt.getModel());
String reportSource = "file/door_reports.JRXML";
Map<String, Object> params = new HashMap<>();
params.put("c1", subtot.getText());
JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);
JasperPrint jasperPrint1 = JasperFillManager.fillReport(jasperReport, params, datasource);
JasperViewer.viewReport(jasperPrint1, false);
error is : error compiling java source file D:/test/dist/Blank_A4_1426091490655_11644
above file do not even exist! anybody know what would be the reason?
Use this method, it should be useful for you:
public static void GenerateReport(String filename, HashMap parameter) {
try {
JasperDesign jspDesign = JRXmlLoader.load(filename);
JasperReport jspReport = JasperCompileManager.compileReport(jspDesign);
JasperPrint jspPrint = JasperFillManager.fillReport(jspReport, parameter, yourconnection);
JasperViewer.viewReport(jspPrint, false);
} catch (Exception e) {
JOptionPane.showConfirmDialog(null, e);
}
}
and be sure of the path of the file, good luck.
Did you rename your jxml after you edited it in Jasper?
The report sets the report file name you chose while creating it in Jasper as name, so if you did simple rename your file and not change the name directly in the jxml too... i think there could be your problem.

Jasper Report not getting the correct path

I have a report in jasper and want to use a logo (gif) that is inside my application (inside /src/main/resources/img)
The Code used to rettrieve the image logo is
public void imprimir(MyReport myreport) throws Exception
{
List myReportList = new ArrayList();
File logo = new File(getClass().getClassLoader().getResource("img/myLogo.gif").getPath());
myreport.setLogo(logo);
myReportList.add(myreport);
FileInputStream fis = (FileInputStream) getClass().getClassLoader().getResourceAsStream("jasper/myreport.jasper");
// JasperReport report = JasperCompileManager.compileReport(fis);
JasperPrint print = JasperFillManager.fillReport(fis, null, new JRBeanCollectionDataSource(myReportList));
JasperExportManager.exportReportToPdfFile(print, "c:/myreport.pdf");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
JasperExportManager.exportReportToPdfStream(print, baos);
DataSource datasource = new ByteArrayDataSource(baos.toByteArray(), "application/pdf");
Email mail = new Email();
mail.setFromLabel("xxxxxxxx#xxxxxxxx.yyy.zz");
mail.setTo("destiny#xxxxxxxx.yyy.zz");
mail.setSubject("myreport");
mail.setMessage("Mesage");
EmailService emailService = new EmailService();
emailService.sendEmail(mail, datasource);
}
But this path does not exists.
[Server:server01] 09:40:12,492 ERROR [stderr] (default task-3) Caused by: java.io.FileNotFoundException: C:\Java\AS\wildfly-8.1.0.Final\content\MyProject.war\WEB-INF\classes\img\logo.gif
So, as it seems, the Path is beeing resolved to a different value.
The deployment is made over a Wildfly 8.1 Final in domain mode (Clustered).
What am I missing here?
Your myLogo.gif has been package in MyProject.war file. The path C:\Java\AS\wildfly-8.1.0.Final\content\MyProject.war\WEB-INF\classes\img\logo.gif isn't exist.
I suggest two solution to resolve this issue.
1.Move myLogo.gif out of MyProject.war. Use real path to load your gif file.
File logo = new File(realPath);
myreport.setLogo(logo);
2.Change the myreport.setLogo(logo) method's parameter type to InputStream.
InputStream logoInputStream = getClass().getClassLoader().getResourceAsStream("img/myLogo.gif");
myreport.setLogoInputStream(logoInputStream);

Jasper Report and Netbeans 7.2

I'm having trouble to load a report through a button.This is my code.When I click the button it shows the down image error.
try {
InputStream in =
getClass().getResourceAsStream("C:/Users/RaMiNdU/Documents/NetBeansProjects/TimeTable
Generator/src/timetable/generator/sem1.jrxml");
JasperDesign jasperDesign = JRXmlLoader.load(in);
Map<String, Object> param = new HashMap<>();
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, param );
JasperViewer.viewReport(jasperPrint, false);
} catch (Exception e) {
System.out.println(e.getMessage());
}
add commons-digester.jar to your classpath and you should be through
commons-digester is not available in the Classpath.
Any start-up script is overriding Classpath environment variable.
Check for java.lang.ExceptionInInitializerError in your log file. NoClassDefFoundError due
to failure of static initialization is quite common.
Your class may not be visible while using multiple class loaders.

Categories