Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to access a file from a EJB  RSS feed

 
Harish Verma
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • 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.
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!