This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
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.
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?
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.