Daniel Ferreira Castro
Daniel Ferreira Castro

Reputation: 344

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("[email protected]");
    mail.setTo("[email protected]");
    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?

Upvotes: 0

Views: 997

Answers (1)

Steven Weng
Steven Weng

Reputation: 322

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);

Upvotes: 1

Related Questions