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

Reply via email to