aspose file tools*
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 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "FileNotFound in Servlet for Tomcat  ? Help !!" Watch "FileNotFound in Servlet for Tomcat  ? Help !!" New topic
Author

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.

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

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


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Ben Souther
Sheriff

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.
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html


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
Sheriff

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
Marshal

Joined: Jan 10, 2002
Posts: 60074
    
  65

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] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: FileNotFound in Servlet for Tomcat ? Help !!
 
Similar Threads
XALAN error
[XSL/FO] displaying date in pdf file
The CATALINA_HOME enviornment variable
Win98SE & Tomcat 4 setup problem
tomcat installation on win98