I read all the questions with answers but still it doesn't work for me.
In my web application I have a jar which contains few images and xmls. These are required by the classes in that jar itself. So I modified the required lines as below:
I changed the classes inside my jar to refer the resources(which are present inside the jar itself) by using :
Thread.currentThread().getContextClassLoader().getResource(path) // returns null
I had also tried before :
MyClassName.class.getResource(path) // null again
I read a lot about this and got to know that the later line of code will be able to pick up the resources, but unfortunately neither of it works for me...am I missing something?
Note: Running on Tomcat7 and since I am running on Vista, I get the 'path' value as '\myFolder\images\'
Thanks
There are few points one must take care when loading resources from within a jar, which is embedded in a web application
File.seperator
it is usually w.r.t Files and absolute paths. It is not recommended for loading resources.File.seperator
and using the normal '/' as it is relative path w.r.t the jar.File.seperator
will not work for loading resources because it will replace '/' with '\' (windows system)Finally, when loading resources in your jar code, use the following lines
MyCurrentClass.class.getResource(path)
Hope it helps people dealing with xmls within jars in a web application