This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Servlets and the fly likes FileNotFound in Servlet for Tomcat  ? Help !! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "FileNotFound in Servlet for Tomcat  ? Help !!" Watch "FileNotFound in Servlet for Tomcat  ? Help !!" New topic

FileNotFound in Servlet for Tomcat ? Help !!

jay vas
Ranch Hand

Joined: Aug 30, 2005
Posts: 407
Hi everyone :

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


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 ?

Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Check out the methods in the File class.

So much trouble in the world -- Bob Marley
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

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.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Jaime M. Tovar
Ranch Hand

Joined: Mar 28, 2005
Posts: 133
I have a question here, i usually use classloader.getresourceasstream to load resources when working in a web app. Is this wrong???

She will remember your heart when men are fairy tales in books written by rabbits.<br /> As long as there is duct tape... there is also hope.
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Jan 10, 2002
Posts: 63873

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.

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: FileNotFound in Servlet for Tomcat ? Help !!
It's not a secret anymore!