You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by jkv <j....@gmail.com> on 2009/12/16 10:39:09 UTC

FileNotFoundException in Tomcat6.0

Hello,

I am using tomcat 6.0 and created a log4j configuration folder under
WEB-INF/classes/log4j.properties. I have my log4j.jar file in WEB-INF/lib
folder. I created a simple servlet where in the init method I have the
following line

PropertyConfigurator.configure("log4j.properties");

but this is throwing FileNotFoundException?? Should my properties file be in
my class path?? Also I created a test setup by creating a sample test.txt
file under WEB-INF/classes folder. When I open the file in my servlet I get
the same FNF Exception??

Any ideas?? Thanks in advance

Regards
Kumaravel J


-- 
View this message in context: http://old.nabble.com/FileNotFoundException-in-Tomcat6.0-tp26808548p26808548.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: FileNotFoundException in Tomcat6.0

Posted by jkv <j....@gmail.com>.
Hello Charles,

I am using a war file named sample.war to deploy my sample application in
tomcat 6.0. I have my log4j.properties file with that exact name in
WEB-INF/classes folder. That is I copy the war file into webapps folder of
tomcat and when I run tomcat it gets explode into folder sample, so I have
the log4j.properties file under in the following path
webapps/sample/WEB-INF/classes/log4j.properties.

Now 
1) when I say PropertyConfigurator.configure("log4j.properties"); I get FNF
exceptions.
2) If I dont say PropertyConfigurator.configure("log4j.properties") in my
servlet init code a directly take a logger instance say
Logger.getlogger(Sample.class); the logging goes on fine.

Note: I have my log4j.jar inside WEB-INF/lib folder and not in tomcat's lib
folder. 


Caldarale, Charles R wrote:
> 
>> From: jkv [mailto:j.kumaravel@gmail.com]
>> Subject: RE: FileNotFoundException in Tomcat6.0
>> 
>> But the problem starts only if you try to configure using a
>> PropertyConfigurator having the same file there and use the 
>> above statement then you get FNFExe because the classpath is
>> acutally bin folder of Tomcat and log4j searches the bin 
>> folder for a log4j.configuration which is actually strange.
> 
> I'm not sure I believe that - log4j should be using the webapp's
> classloader, not the system classloader.  Sounds like a bug in log4j or
> how you have it installed.
> 
> "The getRealPath() method should never be relied upon; the container is
> under no obligation to return anything here.  For example, if your webapp
> is packaged as a .war file, it will return null." -> But I am using a war
> file and I get a valid value instead of null in tomcat. I am getting real
> path up to my application context root say c:/tomcat/webapps/sample/. To
> my knowledge I havent touched any configuration values in tomcat, but I
> will anyways re-check.
> 
> Regards
> jkv
> 
>> So you will have to use this statement 
>> PropertyConfigurator.configure(servletContext.getRealPath("/") +
>> "/WEB-INF/classes/" + "log4j.properties");
> 
> The getRealPath() method should never be relied upon; the container is
> under no obligation to return anything here.  For example, if your webapp
> is packaged as a .war file, it will return null.  You need to fix the
> problem in your log4j setup, not attack the symptom.
> 
>> The similar is the case for any file in Tomcat, ie is if you want to
>> read from a custom file in tomcat say your own configurations for web
>> application the you will have to use the Path for the file using the
>> servletContest.getRealPath("/") + ... your expected file path starting
>> from root.
> 
> That's definitely not true - the classloader path for any thread running
> inside a webapp is WEB-INF/classes and WEB-INF/lib first, followed by the
> shared library, and only then the system classpath.  Something you're
> doing is breaking the classloader model implemented by Tomcat:
> http://tomcat.apache.org/tomcat-6.0-doc/class-loader-howto.html
> 
>  - 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
> 
> 
> 

-- 
View this message in context: http://old.nabble.com/FileNotFoundException-in-Tomcat6.0-tp26808548p26842784.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: FileNotFoundException in Tomcat6.0

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: jkv [mailto:j.kumaravel@gmail.com]
> Subject: RE: FileNotFoundException in Tomcat6.0
> 
> But the problem starts only if you try to configure using a
> PropertyConfigurator having the same file there and use the 
> above statement then you get FNFExe because the classpath is
> acutally bin folder of Tomcat and log4j searches the bin 
> folder for a log4j.configuration which is actually strange.

I'm not sure I believe that - log4j should be using the webapp's classloader, not the system classloader.  Sounds like a bug in log4j or how you have it installed.

> So you will have to use this statement 
> PropertyConfigurator.configure(servletContext.getRealPath("/") +
> "/WEB-INF/classes/" + "log4j.properties");

The getRealPath() method should never be relied upon; the container is under no obligation to return anything here.  For example, if your webapp is packaged as a .war file, it will return null.  You need to fix the problem in your log4j setup, not attack the symptom.

> The similar is the case for any file in Tomcat, ie is if you want to
> read from a custom file in tomcat say your own configurations for web
> application the you will have to use the Path for the file using the
> servletContest.getRealPath("/") + ... your expected file path starting
> from root.

That's definitely not true - the classloader path for any thread running inside a webapp is WEB-INF/classes and WEB-INF/lib first, followed by the shared library, and only then the system classpath.  Something you're doing is breaking the classloader model implemented by Tomcat:
http://tomcat.apache.org/tomcat-6.0-doc/class-loader-howto.html

 - 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: FileNotFoundException in Tomcat6.0

Posted by jkv <j....@gmail.com>.
Thanks Charles for the reply..,

I got the answer myself after a long time... Actually its like this

IF you have a default log file for log4j configuration that is
"log4j.configuration" under WEB-INF/classes folder and did not use this
statement
PropertyConfigurator.configure("log4j.properties"); then the system works
fine, I dont know how but log4j is intelligent to pick the file
"log4j.properties" from the classes folder.

But the problem starts only if you try to configure using a
PropertyConfigurator having the same file there and use the above statement
then you get FNFExe because the classpath is acutally bin folder of Tomcat
and log4j searches the bin folder for a log4j.configuration which is
actually strange. So you will have to use this statement

PropertyConfigurator.configure(servletContext.getRealPath("/") +
"/WEB-INF/classes/" + "log4j.properties");

The similar is the case for any file in Tomcat, ie is if you want to read
from a custom file in tomcat say your own configurations for web application
the you will have to use the Path for the file using the
servletContest.getRealPath("/") + ... your expected file path starting from
root.

Thanks for your valuable time
Regards
JKV


Caldarale, Charles R wrote:
> 
>> From: jkv [mailto:j.kumaravel@gmail.com]
>> Subject: FileNotFoundException in Tomcat6.0
>> 
>> I created a simple servlet where in the init method I have the
>> following line
>> 
>> PropertyConfigurator.configure("log4j.properties");
>> 
>> but this is throwing FileNotFoundException??
> 
> Let's see the stack trace.  Does the Tomcat userid have access to the
> file?
> 
>> Should my properties file be in my class path??
> 
> Never, never, never use the CLASSPATH environment variable, especially
> with Tomcat.  The Tomcat startup scripts set the -cp parameter properly;
> don't muck with it.
> 
>  - 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
> 
> 
> 

-- 
View this message in context: http://old.nabble.com/FileNotFoundException-in-Tomcat6.0-tp26808548p26827295.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: FileNotFoundException in Tomcat6.0

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: jkv [mailto:j.kumaravel@gmail.com]
> Subject: FileNotFoundException in Tomcat6.0
> 
> I created a simple servlet where in the init method I have the
> following line
> 
> PropertyConfigurator.configure("log4j.properties");
> 
> but this is throwing FileNotFoundException??

Let's see the stack trace.  Does the Tomcat userid have access to the file?

> Should my properties file be in my class path??

Never, never, never use the CLASSPATH environment variable, especially with Tomcat.  The Tomcat startup scripts set the -cp parameter properly; don't muck with it.

 - 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: FileNotFoundException in Tomcat6.0

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Kumaravel,

On 12/18/2009 7:45 AM, jkv wrote:
> I was actually not specifing any path to open a file "text.txt" as i had it
> under
> WEB-INF/classes/test.txt, assuming my classes folder should be in classpath.

While your file was in the class path, you aren't using a method that
uses the class path.

> So I started to use getRealPath("/"), not sure of the implications though.

As has been said several times over the last few days/weeks on this
list, the use of getRealPath() is strongly discouraged.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAksrv9gACgkQ9CaO5/Lv0PCOLQCggB17VBE/Qnm3Qpm+D2bBvGG7
ntUAoLdgFrxPg0bBHpTQLalDEDHH+MLH
=BQtI
-----END PGP SIGNATURE-----

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


Re: FileNotFoundException in Tomcat6.0

Posted by jkv <j....@gmail.com>.
Hello,

"This method takes a filename. Since you've not provided a path to the
file, here, where did you put your file to ensure that it gets properly
loaded?" -> I have my file here WEB-INF/classes/log4j.properties.

"What path are you using when you try to open the file? If you aren't
fully-specifying the path, then the path is relative. Relative to what?
Relative to the current working directory of the JVM process. What's the
CWD? It could be anything, though it tends to be either the CWD of the
parent process or CATALINA_BASE. You shouldn't have to worry about that,
though, because you should always fully-specify your file paths."

I was actually not specifing any path to open a file "text.txt" as i had it
under
WEB-INF/classes/test.txt, assuming my classes folder should be in classpath.
But you are right here - >  "If you aren't
fully-specifying the path, then the path is relative. Relative to what?
Relative to the current working directory of the JVM process." which is the
bin folder of tomcat ofcourse.
So I started to use getRealPath("/"), not sure of the implications though.

Regards
jkv



Christopher Schultz-2 wrote:
> 
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Kumaravel,
> 
> On 12/16/2009 4:39 AM, jkv wrote:
>> I am using tomcat 6.0 and created a log4j configuration folder under
>> WEB-INF/classes/log4j.properties. I have my log4j.jar file in WEB-INF/lib
>> folder. I created a simple servlet where in the init method I have the
>> following line
>> 
>> PropertyConfigurator.configure("log4j.properties");
> 
> This method takes a filename. Since you've not provided a path to the
> file, here, where did you put your file to ensure that it gets properly
> loaded?
> 
>> but this is throwing FileNotFoundException?? Should my properties file be
>> in
>> my class path?
> 
> That depends on how you configure log4j. We simply do this:
> 
> new PropertyConfigurator();
> 
> ...and put log4j.properties into the CLASSPATH by dropping it into the
> WEB-INF/classes directory in our webapp.
> 
>> Also I created a test setup by creating a sample test.txt
>> file under WEB-INF/classes folder. When I open the file in my servlet I
>> get
>> the same FNF Exception??
> 
> What path are you using when you try to open the file? If you aren't
> fully-specifying the path, then the path is relative. Relative to what?
> Relative to the current working directory of the JVM process. What's the
> CWD? It could be anything, though it tends to be either the CWD of the
> parent process or CATALINA_BASE. You shouldn't have to worry about that,
> though, because you should always fully-specify your file paths.
> 
> Better yet, load your file using the ClassLoader.
> 
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.10 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
> 
> iEYEARECAAYFAksqomkACgkQ9CaO5/Lv0PA7MQCgsLVY4bVhSW3ijmaTemfTb5OK
> 9m4An1gOgbdkFI7OYHqmRCMIXA/Zw2nI
> =eLMQ
> -----END PGP SIGNATURE-----
> 
> ---------------------------------------------------------------------
> 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://old.nabble.com/FileNotFoundException-in-Tomcat6.0-tp26808548p26842857.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: FileNotFoundException in Tomcat6.0

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Kumaravel,

On 12/16/2009 4:39 AM, jkv wrote:
> I am using tomcat 6.0 and created a log4j configuration folder under
> WEB-INF/classes/log4j.properties. I have my log4j.jar file in WEB-INF/lib
> folder. I created a simple servlet where in the init method I have the
> following line
> 
> PropertyConfigurator.configure("log4j.properties");

This method takes a filename. Since you've not provided a path to the
file, here, where did you put your file to ensure that it gets properly
loaded?

> but this is throwing FileNotFoundException?? Should my properties file be in
> my class path?

That depends on how you configure log4j. We simply do this:

new PropertyConfigurator();

...and put log4j.properties into the CLASSPATH by dropping it into the
WEB-INF/classes directory in our webapp.

> Also I created a test setup by creating a sample test.txt
> file under WEB-INF/classes folder. When I open the file in my servlet I get
> the same FNF Exception??

What path are you using when you try to open the file? If you aren't
fully-specifying the path, then the path is relative. Relative to what?
Relative to the current working directory of the JVM process. What's the
CWD? It could be anything, though it tends to be either the CWD of the
parent process or CATALINA_BASE. You shouldn't have to worry about that,
though, because you should always fully-specify your file paths.

Better yet, load your file using the ClassLoader.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAksqomkACgkQ9CaO5/Lv0PA7MQCgsLVY4bVhSW3ijmaTemfTb5OK
9m4An1gOgbdkFI7OYHqmRCMIXA/Zw2nI
=eLMQ
-----END PGP SIGNATURE-----

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