I still have questions about the path to a file. {This is within a servlet... actually in my case I am trying to read/write from a ContextListenr} I understand that just opening a FIle will be relative to where Tomcat was started. Is there a simple java method (and on what class) that will give me the correct path to 'webapps/MyApplication/'. As recommended below, you could use Class.getResourcesStream(), but as I understand it, that will give the location of this class under WEB-INF. And that assumes you are calling it from one of your own classes and the loader picked up your class.

I got the following technique from a book, but I am not in love with it:

public void contextInitialized(ServletContextEvent event)  {
   ServletContext sctx = event.getServletContext();
   String propPath = sctx.getRealPath( "/WEB-INF/resource.properties");
   FileInputStream inStrm = new FileInputStream(propPath);
 .....
}

Is this the proper way to do it?

-d

Caldarale, Charles R wrote:
From: popprem [mailto:popp...@gmail.com] Subject: Path problem

David answered the critical part of your question, but I thought I'd take a 
crack at the rest.

That means tomcat defaultly points to bin directory

"Tomcat" doesn't defaultly [sic] point to anything.  The current directory of 
the process you used to start Tomcat when using the script happened to be Tomcat's bin 
directory - but that's just the way you're using the script, not a Tomcat requirement.

When i used tomcat.exe & installed tomcat in my machine & run the program,i found that the test file i created in my code level was in windows/system32/ directory.

Because that's the default current directory for services.

It's almost always a bad idea to depend on the current directory setting in a 
complex server environment; if you must have a file system path to something, 
better to use one based off a system property.  In your case, the 
getResourceAsStream() method is much preferred, since it elinates any file 
system dependencies.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY 
MATERIAL and is thus for use only by the intended recipient. If you received 
this in error, please contact the sender and delete the e-mail and its 
attachments from all computers.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org


Reply via email to