• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to access a file from a EJB

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for a very basic question. (Am just getting started with J2EE.)

I have a EJB session bean that lives in a package called org.rphl.ejb

I have a file called MyFirst.jasper that represents compiled XML file for creating a jasper report. I have placed it under folder jasperreports under WEB-INF.

-WEB-INF --> jasperreports --> MyReport.jasper

In my EJB I wrote:

File reportFile = new File("jasperreports/MyFirst.jasper");

JasperRunManager.runReportToPdfFile(reportFile.getPath(), new HashMap(), new JRResultSetDataSource(rs));

(where rs is a resultset, defined correctly)

I get java.io.FileNotFoundException: jasperreports\MyFirstReport.jasper

Pls tell me how to properly access a file from anywhere in the J2EE tier. TIA.
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are creating the File instance with a relative path to the working directory of the application server. It is extremely unlikely that the working directory will be anywhere near WEB-INF/jasperreports/MyReport.jasper.
What I do when I need external resources is place them somewhere that makes sense on the file system then make an initalization parameter with the full path. It's not too difficult to make parameters user or system dependent, should you need to change the location for different deployments.
Alternatively, you could put your report somewhere on the classpath and obtain a reference to it with Class.getResource() or Class.getResourceAsStream().
 
Harish Verma
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Joe Ess:
You are creating the File instance with a relative path to the working directory of the application server. It is extremely unlikely that the working directory will be anywhere near WEB-INF/jasperreports/MyReport.jasper.
What I do when I need external resources is place them somewhere that makes sense on the file system then make an initalization parameter with the full path. It's not too difficult to make parameters user or system dependent, should you need to change the location for different deployments.
Alternatively, you could put your report somewhere on the classpath and obtain a reference to it with Class.getResource() or Class.getResourceAsStream().



Thanks much - this resolved my problem.
reply
    Bookmark Topic Watch Topic
  • New Topic