Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

FileNotFound in Servlet for Tomcat ? Help !!

 
jay vas
Ranch Hand
Posts: 407
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone :

When Im running a web app in tomcat, I cant refer locally to
a file i.e.

FileUtilities.readFileContents("C:\\xsl\\clinicalInfo.xsl");

Does NOT work and results in a file not found exception.

But On the other hand

FileUtilities.readFileContents("C:\\Documents and Settings\\Administrator\\workspace\\CS\\xsl\\clinicalInfo.xsl");

Works just fine.

How can I find out the directory in which the code is executing so that
I can know what path to put when reading files ?

Thanks
Jay
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the methods in the File class.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shouldn't rely on the current working directory when writing webapps.

If the file is to be packaged with your webapp, use ServletContext.getResource or getResourceAsStream. Both methods will locate the file from a path relative to the context root.
If not, use an absolute path.
You can make this path configurable via a context or servlet init-param.


Note: Depending on your needs, the ServletContext.getRealPath method may be a sufficient hack. I say hack because it's not really a portable solution.
It will return null if called from within an app running from a packed war file.
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html
 
Jaime M. Tovar
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question here, i usually use classloader.getresourceasstream to load resources when working in a web app. Is this wrong???
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Tovar:
I have a question here, i usually use classloader.getresourceasstream to load resources when working in a web app. Is this wrong???


I can't say with any authority since I've never used the class loader to get resources in webapps.
What I do know is that webservers often use custom classloaders for webapps (Tomcat does) and the behaviour of the class loaders (as far as getting static resources) is not rigidly defined in the servlet spec. It doesn't need to be since ServletContext has methods defined for doing this.
Since it's not rigidly defined, I wouldn't feel comfortable distributing an app that uses it unless I've personally tested it on every container and container version (+ the various operating systems) that I indended to support.

Out of curiosity: why did you choose to use the classLoader instead of the ServletContext for getting resources?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65220
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The getResource mechanism for the ServletContext locates resources relative to the context path. The class-loader getResource mechanism locates resources on the classpath. I've used the latter on many occasions to locate resources bundled in jar files.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic