You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by popprem <po...@gmail.com> on 2009/03/13 06:51:21 UTC

Path problem

 
 Hi,

 I'v really stuck with an issue. I have done a web program using struts
framework. I have a plugin class there for which i have to pass a
configuration file as parameter. That file is in webapps/Myapp/WEB-INF/
directory. So i mentioned relative path as
../webapps/Myapp/WEB-INF/conf.xml. it works fine if i download tomcat zip
file & extract it & use. Bt if i download a .exe file (eg:
Apache-tomcat-5.5.15.exe) & run it to install & use it, struts doesnt get
that path. 

I'm really stuck with this prob. i need a way to specify path which works
for both tomcat installations.

Please help me to solve this prob.

Note: 
I thought to check where a file creates if i create a file from my program.
When the tomcat.zip is used i found the created test file is in bin
directory of tomcat. That means tomcat defaultly points to bin directory, so
the path to the config file ../webapps/Myapp/WEB-INF/conf.xml. works fine.
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. so in this case tomcat doesnt defaultly points to the bin
directory i guess.

hope this helps to give me a good answer.
Thanks in advance.


-- 
View this message in context: http://www.nabble.com/Path-problem-tp22490662p22490662.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


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


Re: Path problem

Posted by David Smith <dn...@cornell.edu>.
popprem wrote:
>  
>  Hi,
>
>  I'v really stuck with an issue. I have done a web program using struts
> framework. I have a plugin class there for which i have to pass a
> configuration file as parameter. That file is in webapps/Myapp/WEB-INF/
> directory. So i mentioned relative path as
> ../webapps/Myapp/WEB-INF/conf.xml. it works fine if i download tomcat zip
> file & extract it & use. Bt if i download a .exe file (eg:
> Apache-tomcat-5.5.15.exe) & run it to install & use it, struts doesnt get
> that path. 
>
> I'm really stuck with this prob. i need a way to specify path which works
> for both tomcat installations.
>
> Please help me to solve this prob.
>
> Note: 
> I thought to check where a file creates if i create a file from my program.
> When the tomcat.zip is used i found the created test file is in bin
> directory of tomcat. That means tomcat defaultly points to bin directory, so
> the path to the config file ../webapps/Myapp/WEB-INF/conf.xml. works fine.
> 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. so in this case tomcat doesnt defaultly points to the bin
> directory i guess.
>
> hope this helps to give me a good answer.
> Thanks in advance.
>
>
>   
Check the docs for your plugin to see if there's any other way of
expressing the path to your config file.  The standard method of reading
any file in a webapp is to use ServletContext.getResourceAsStream() to
get an input stream and then use that to read the file.  Doing that, the
path would be "/WEB-INF/conf/.xml".  I'd be surprised of a Struts plugin
doesn't accept something like that.

--David


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


Re: Path problem

Posted by popprem <po...@gmail.com>.
Hi,

ln my case i the config file is not in any of the jars, also i cant set it
to the classpath for some reason. (This product runs in different machines
connecting through RMI). My config file just resides inside WEB-INF. so if i
want to set it as a system property what can i do?

The java classes i run are inside a jar which is in WEB-INF/lib directory.


David Smith-2 wrote:
> 
> To demystify things:
> 
> 1. ServletContext.getResourceAsStream("/path/inside/webapp.blah")
> returns an object that supports the java.io.InputStream stream
> interface.  After you get the stream, you can treat it like any other
> input stream in the java world.
> 
> 2. The path is rooted in the *webapp*, not the file system.  Given a
> file in a webapp like
> $CATALINA_HOME/webapps/mywebapp/WEB-INF/myconfigs/myconfig.properties,
> the path you'd use with ServletContext.getResourceAsStream() would be
> "/WEB-INF/myconfigs/myconfig.properties".
> 
> 3. Logs would be a different story.  You are best off configuring your
> log tool according to it's documentation and let it handle the logging.
> 
> --David
> 
> David.Meldrum wrote:
>> OK, I agree about writing, but what about reading?  I guess I didn't
>> realize there was a method called getResourcesStream(), on
>> ServletContext, so that clears up some confusion.  Yet when I read the
>> javadoc on ServletContext.getRessourceAsStream()
>> see:
>> http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletContext.html#getResourceAsStream(java.lang.String)
>> <http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletContext.html#getResourceAsStream%28java.lang.String%29>
>>
>> It still isn't clear how to properly use this.  For example, how do I
>> get a reference to the "/webapps/MyWebApp/WEB-INF/foo.properties" file?
>> Like this?
>> ServletContext.getRessourceAsStream("/webapps/MyWebApp/WEB-INF/foo.properties");?
>>
>> The javadoc says the argument is a path, but I don't know the full
>> path, only relative to Tomcat, and I don't really know where Tomcat is.
>> Also how do I create a file under "/logs"?
>> Is there some way to determine the path to the /logs directory in a
>> format that I can create a file under the logs
>> directory, without assuming where Tomcat is deployed?
>>
>> -d
>>
>> Caldarale, Charles R wrote:
>>>> From: David.Meldrum [mailto:david.meldrum@verizon.net] Subject: Re:
>>>> Path problem
>>>>
>>>> actually in my case I am trying to read/write from a ContextListenr
>>>>     
>>>
>>> It's generally a bad idea to ever *write* into the webapp deployment
>>> space.  You have no guarantee that the space is writable, nor that
>>> the container will provide any write access.  You're much better off
>>> writing to files outside of Tomcat's directory structure, where the
>>> path can be provided by system property, environment variable,
>>> <init-param>, or whatever.
>>>
>>>  
>>>> As recommended below, you could use Class.getResourcesStream(), but
>>>> as I understand it,
>>>> that will give the location of this class under WEB-INF.
>>>>     
>>>
>>> Or anywhere else in the webapp's structure; it's not confined to
>>> WEB-INF.
>>>
>>>  
>>>> public void contextInitialized(ServletContextEvent event)  {
>>>>     ServletContext sctx = event.getServletContext();
>>>>     String propPath = sctx.getRealPath(
>>>> "/WEB-INF/resource.properties");
>>>>     FileInputStream inStrm = new FileInputStream(propPath);
>>>>   .....
>>>> }
>>>>     
>>>
>>> Using ServletContext.getRealPath() is risky; the container is under
>>> no obligation to provide access to the underlying file system (think
>>> deployment via .war file).  Much safer and better to use
>>> getResourceAsStream().
>>>
>>>  - Chuck
>>>
>>>
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Path-problem-tp22490662p22535464.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


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


Re: Path problem

Posted by David Smith <dn...@cornell.edu>.
To demystify things:

1. ServletContext.getResourceAsStream("/path/inside/webapp.blah")
returns an object that supports the java.io.InputStream stream
interface.  After you get the stream, you can treat it like any other
input stream in the java world.

2. The path is rooted in the *webapp*, not the file system.  Given a
file in a webapp like
$CATALINA_HOME/webapps/mywebapp/WEB-INF/myconfigs/myconfig.properties,
the path you'd use with ServletContext.getResourceAsStream() would be
"/WEB-INF/myconfigs/myconfig.properties".

3. Logs would be a different story.  You are best off configuring your
log tool according to it's documentation and let it handle the logging.

--David

David.Meldrum wrote:
> OK, I agree about writing, but what about reading?  I guess I didn't
> realize there was a method called getResourcesStream(), on
> ServletContext, so that clears up some confusion.  Yet when I read the
> javadoc on ServletContext.getRessourceAsStream()
> see:
> http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletContext.html#getResourceAsStream(java.lang.String)
> <http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletContext.html#getResourceAsStream%28java.lang.String%29>
>
> It still isn't clear how to properly use this.  For example, how do I
> get a reference to the "/webapps/MyWebApp/WEB-INF/foo.properties" file?
> Like this?
> ServletContext.getRessourceAsStream("/webapps/MyWebApp/WEB-INF/foo.properties");?
>
> The javadoc says the argument is a path, but I don't know the full
> path, only relative to Tomcat, and I don't really know where Tomcat is.
> Also how do I create a file under "/logs"?
> Is there some way to determine the path to the /logs directory in a
> format that I can create a file under the logs
> directory, without assuming where Tomcat is deployed?
>
> -d
>
> Caldarale, Charles R wrote:
>>> From: David.Meldrum [mailto:david.meldrum@verizon.net] Subject: Re:
>>> Path problem
>>>
>>> actually in my case I am trying to read/write from a ContextListenr
>>>     
>>
>> It's generally a bad idea to ever *write* into the webapp deployment
>> space.  You have no guarantee that the space is writable, nor that
>> the container will provide any write access.  You're much better off
>> writing to files outside of Tomcat's directory structure, where the
>> path can be provided by system property, environment variable,
>> <init-param>, or whatever.
>>
>>  
>>> As recommended below, you could use Class.getResourcesStream(), but
>>> as I understand it,
>>> that will give the location of this class under WEB-INF.
>>>     
>>
>> Or anywhere else in the webapp's structure; it's not confined to
>> WEB-INF.
>>
>>  
>>> public void contextInitialized(ServletContextEvent event)  {
>>>     ServletContext sctx = event.getServletContext();
>>>     String propPath = sctx.getRealPath(
>>> "/WEB-INF/resource.properties");
>>>     FileInputStream inStrm = new FileInputStream(propPath);
>>>   .....
>>> }
>>>     
>>
>> Using ServletContext.getRealPath() is risky; the container is under
>> no obligation to provide access to the underlying file system (think
>> deployment via .war file).  Much safer and better to use
>> getResourceAsStream().
>>
>>  - Chuck
>>
>>


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


RE: Path problem

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: David.Meldrum [mailto:david.meldrum@verizon.net] 
> Subject: Re: Path problem
> 
> For example, how do I get a reference to the 
> "/webapps/MyWebApp/WEB-INF/foo.properties" file?

Gregor already told you here:
http://marc.info/?l=tomcat-user&m=123697177925016&w=2

> ServletContext.getRessourceAsStream("/webapps/MyWebApp/WEB-INF
> /foo.properties");?

No, leave out the /webapps.

> The javadoc says the argument is a path

Read the doc more carefully; it actually says:

"The path must begin with a '/' and is interpreted as relative to the current context root."

> but I don't know the full path, 

You don't need to; the path is relative to your webapp (despite the leading slash).

> Also how do I create a file under "/logs"?

Usually, you use a logger.  Tomcat provides a built-in logging mechanism based on java.util.logging, or you can implement one based on any of several standard libraries, such as log4j.  The logger configuration specifies where to write the output.

 - 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-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Path problem

Posted by "David.Meldrum" <da...@verizon.net>.
OK, I agree about writing, but what about reading?  I guess I didn't 
realize there was a method called getResourcesStream(), on 
ServletContext, so that clears up some confusion.  Yet when I read the 
javadoc on ServletContext.getRessourceAsStream()
see:
http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletContext.html#getResourceAsStream(java.lang.String) 
<http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletContext.html#getResourceAsStream%28java.lang.String%29>
It still isn't clear how to properly use this.  For example, how do I 
get a reference to the "/webapps/MyWebApp/WEB-INF/foo.properties" file?
Like this?
ServletContext.getRessourceAsStream("/webapps/MyWebApp/WEB-INF/foo.properties");?
The javadoc says the argument is a path, but I don't know the full path, 
only relative to Tomcat, and I don't really know where Tomcat is.
Also how do I create a file under "/logs"?
Is there some way to determine the path to the /logs directory in a 
format that I can create a file under the logs
directory, without assuming where Tomcat is deployed?

-d

Caldarale, Charles R wrote:
>> From: David.Meldrum [mailto:david.meldrum@verizon.net] 
>> Subject: Re: Path problem
>>
>> actually in my case I am trying to read/write from a 
>> ContextListenr
>>     
>
> It's generally a bad idea to ever *write* into the webapp deployment space.  You have no guarantee that the space is writable, nor that the container will provide any write access.  You're much better off writing to files outside of Tomcat's directory structure, where the path can be provided by system property, environment variable, <init-param>, or whatever.
>
>   
>> As recommended below, you could use 
>> Class.getResourcesStream(), but as I understand it,
>> that will give the location of this class under 
>> WEB-INF.
>>     
>
> Or anywhere else in the webapp's structure; it's not confined to WEB-INF.
>
>   
>> public void contextInitialized(ServletContextEvent event)  {
>>     ServletContext sctx = event.getServletContext();
>>     String propPath = sctx.getRealPath( 
>> "/WEB-INF/resource.properties");
>>     FileInputStream inStrm = new FileInputStream(propPath);
>>   .....
>> }
>>     
>
> Using ServletContext.getRealPath() is risky; the container is under no obligation to provide access to the underlying file system (think deployment via .war file).  Much safer and better to use getResourceAsStream().
>
>  - 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-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>
>   

Re: Path problem

Posted by Gregor Schneider <rc...@googlemail.com>.
...and please forgive me all my typos....

Rgds

Gregor
-- 
just because your paranoid, doesn't mean they're not after you...
gpgp-fp: 79A84FA526807026795E4209D3B3FE028B3170B2
gpgp-key available @ http://pgpkeys.pca.dfn.de:11371

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


Re: Path problem

Posted by Gregor Schneider <rc...@googlemail.com>.
Or the war-file doesn't get unpacked, but the app is started from
within the war, and the ressource is inside the war.

And please note the difference:

It's not Class.getRessourceAsStream(), but
ServletContext.getRessourceAsStream();

So in your example:

ServletContext sctx = event.getServletContext();

InpuStream is = sctx.getRessourceAsStream("/WEB-INF/yourFileHere.props").

Please also do note, that according to the servlet-specs the
parameter-String in ServletContext.getRessourceAsStream() alsways has
to start with a slash ("/").

Rgds

Gregor
-- 
just because your paranoid, doesn't mean they're not after you...
gpgp-fp: 79A84FA526807026795E4209D3B3FE028B3170B2
gpgp-key available @ http://pgpkeys.pca.dfn.de:11371

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


Re: Path problem

Posted by André Warnier <aw...@ice-sa.com>.
Caldarale, Charles R wrote:
[...]
> 
> It's generally a bad idea to ever *write* into the webapp deployment space.  You have no guarantee that the space is writable, nor that the container will provide any write access.  You're much better off writing to files outside of Tomcat's directory structure, where the path can be provided by system property, environment variable, <init-param>, or whatever.
> 
Just elaborating a bit : what Chuck means is that this Tomcat could be 
running on his iPhone, or the webapps directory may be located on a 
read-only filesystem.

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


RE: Path problem

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: David.Meldrum [mailto:david.meldrum@verizon.net] 
> Subject: Re: Path problem
> 
> actually in my case I am trying to read/write from a 
> ContextListenr

It's generally a bad idea to ever *write* into the webapp deployment space.  You have no guarantee that the space is writable, nor that the container will provide any write access.  You're much better off writing to files outside of Tomcat's directory structure, where the path can be provided by system property, environment variable, <init-param>, or whatever.

> As recommended below, you could use 
> Class.getResourcesStream(), but as I understand it,
> that will give the location of this class under 
> WEB-INF.

Or anywhere else in the webapp's structure; it's not confined to WEB-INF.

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

Using ServletContext.getRealPath() is risky; the container is under no obligation to provide access to the underlying file system (think deployment via .war file).  Much safer and better to use getResourceAsStream().

 - 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-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Path problem

Posted by "David.Meldrum" <da...@verizon.net>.
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:popprem@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-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>
>   

RE: Path problem

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: popprem [mailto:popprem@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-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org