You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Lin Chun <fr...@gmail.com> on 2009/08/12 15:43:55 UTC

Why myApps classpath is tomcat/common

Hi,
I have a java class packaged in a jar in  /myApp/WEB-INF/lib
In this class I have to get a resouce ,

ClassLoader.getSystemResource(CONFIG_FILE_NAME).toURI())


I get null of this, when I trace the pb I find that the current path of
class was "tomcat/common" but not myApp/WEB-INF/lib


regard,




-- 
-------------------------
Lin Chun
Samuel Goldwyn<http://www.brainyquote.com/quotes/authors/s/samuel_goldwyn.html>
- "I'm willing to admit that I may not always be right, but I am never
wrong."

RE: Why myApps classpath is tomcat/common

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Christopher Schultz [mailto:chris@christopherschultz.net]
> Subject: Re: Why myApps classpath is tomcat/common
> 
> Note that this uses the "system" ClassLoader to locate resources. I
> suspect the CLASSPATH for that loader is something like
> /opt/sun-jdk-x-y-z/lib/rt.jar plus a few other things.

No, the system classloader is the one used to load the primary application classes; when running Tomcat, it normally references Tomcat's bin/bootstrap.jar file plus the jars linked from that one via META-INF.  The rt.jar and other JRE jars are handled by the JVM's bootstrap (aka "null") classloader.

 - 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.


RE: Why myApps classpath is tomcat/common

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Lin Chun [mailto:franks1984@gmail.com]
> Subject: Re: Why myApps classpath is tomcat/common
> 
> I log the classpath by
> 
> MyClass.class.getClassLoader().getResource("/")
> Thread.currentThread().getContextClassLoader().getResource("")

I see no logging here.

You would likely be better off using getResourceAsStream(), since the URL returned by getResource() may not be useful.

 - 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: Why myApps classpath is tomcat/common

Posted by Lin Chun <fr...@gmail.com>.
hi,
I log the classpath by

MyClass.class.getClassLoader().getResource("/")
Thread.currentThread().getContextClassLoader().getResource("")


On Wed, Aug 12, 2009 at 6:51 PM, Caldarale, Charles R <
Chuck.Caldarale@unisys.com> wrote:

> > From: Lin Chun [mailto:franks1984@gmail.com]
> > Subject: Re: Why myApps classpath is tomcat/common
> >
> > this.getClass().getClassLoader().getResource()
> > Thread.currentThread().getClassContext().getResource()
> >
> > they were all pointed to tomcat/common
>
> How did you determine that they were all pointed to tomcat/common?  (Please
> be very specific.)
>
>  - 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
>
>


-- 
-------------------------
Lin Chun
Stephen Leacock<http://www.brainyquote.com/quotes/authors/s/stephen_leacock.html>
- "I detest life-insurance agents: they always argue that I shall some
day
die, which is not so."

RE: Why myApps classpath is tomcat/common

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Lin Chun [mailto:franks1984@gmail.com]
> Subject: Re: Why myApps classpath is tomcat/common
> 
> this.getClass().getClassLoader().getResource()
> Thread.currentThread().getClassContext().getResource()
> 
> they were all pointed to tomcat/common

How did you determine that they were all pointed to tomcat/common?  (Please be very specific.)

 - 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: Why myApps classpath is tomcat/common

Posted by Lin Chun <fr...@gmail.com>.
hi chris
I've turned to

this.getClass().getClassLoader().getResource()
Thread.currentThread().getClassContext().getResource()

they were all pointed to tomcat/common
finally I uncompressed the jar to /myApps/WEB-INF/classes, that solved the
problem.
i follow the doc of tomcat,  the jars under /WEB-INF/lib are not loaded by
COMMON LOADER,I still don't know why the classpath is in common

Re,

On Wed, Aug 12, 2009 at 4:54 PM, Christopher Schultz <
chris@christopherschultz.net> wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Lin,
>
> On 8/12/2009 9:43 AM, Lin Chun wrote:
> > I have a java class packaged in a jar in  /myApp/WEB-INF/lib
> > In this class I have to get a resouce ,
> >
> > ClassLoader.getSystemResource(CONFIG_FILE_NAME).toURI())
>
> Note that this uses the "system" ClassLoader to locate resources. I
> suspect the CLASSPATH for that loader is something like
> /opt/sun-jdk-x-y-z/lib/rt.jar plus a few other things. No Tomcat stuff.
> No webapp libraries or WEB-INF/classes or anything like that.
>
> > I get null of this, when I trace the pb I find that the current path of
> > class was "tomcat/common" but not myApp/WEB-INF/lib
>
> Do you mean that the "current working directory" of the JVM process is
> tomcat/common? That's not surprising: Tomcat probably never changes its
> working directory for any reason.
>
> The CWD is pretty much irrelevant if you want to load things from the
> ClassLoader.
>
> I don't think you want to use the system ClassLoader, so let me suggest
> an alternative:
>
> InputStream configFile = this.getClass().getClassLoader()
>                          .getResourceAsStream(CONFIG_FILE_NAME);
>
> This will work if your config file is rooted in your webapp's root (like
> webapps/myApp). If your config file is located in
> .../webapps/myApp/WEB-INF/config.xml then you want to set
> CONFIG_FILE_NAME to "/WEB-INF/config.xml".
>
> Remember that the input stream returned from getResourceAsStream can be
> null if the resource is not found (instead of an exception being
> thrown), so you should check the return value before using it.
>
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.9 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAkqC14oACgkQ9CaO5/Lv0PAd6ACdFF4jNr2FUn60Dm25HQgmbESk
> tioAnjFD8a4hHFjphOWV0IV9vmBN/A2c
> =mowI
> -----END PGP SIGNATURE-----
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>


-- 
-------------------------
Lin Chun
Joan Crawford<http://www.brainyquote.com/quotes/authors/j/joan_crawford.html>
- "I, Joan Crawford, I believe in the dollar. Everything I earn, I
spend."

Re: Why myApps classpath is tomcat/common

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

Lin,

On 8/12/2009 9:43 AM, Lin Chun wrote:
> I have a java class packaged in a jar in  /myApp/WEB-INF/lib
> In this class I have to get a resouce ,
> 
> ClassLoader.getSystemResource(CONFIG_FILE_NAME).toURI())

Note that this uses the "system" ClassLoader to locate resources. I
suspect the CLASSPATH for that loader is something like
/opt/sun-jdk-x-y-z/lib/rt.jar plus a few other things. No Tomcat stuff.
No webapp libraries or WEB-INF/classes or anything like that.

> I get null of this, when I trace the pb I find that the current path of
> class was "tomcat/common" but not myApp/WEB-INF/lib

Do you mean that the "current working directory" of the JVM process is
tomcat/common? That's not surprising: Tomcat probably never changes its
working directory for any reason.

The CWD is pretty much irrelevant if you want to load things from the
ClassLoader.

I don't think you want to use the system ClassLoader, so let me suggest
an alternative:

InputStream configFile = this.getClass().getClassLoader()
                          .getResourceAsStream(CONFIG_FILE_NAME);

This will work if your config file is rooted in your webapp's root (like
webapps/myApp). If your config file is located in
.../webapps/myApp/WEB-INF/config.xml then you want to set
CONFIG_FILE_NAME to "/WEB-INF/config.xml".

Remember that the input stream returned from getResourceAsStream can be
null if the resource is not found (instead of an exception being
thrown), so you should check the return value before using it.

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

iEYEARECAAYFAkqC14oACgkQ9CaO5/Lv0PAd6ACdFF4jNr2FUn60Dm25HQgmbESk
tioAnjFD8a4hHFjphOWV0IV9vmBN/A2c
=mowI
-----END PGP SIGNATURE-----

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