File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Websphere and the fly likes How does getClassLoader().getResourceAsStream() work? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Websphere
Bookmark "How does getClassLoader().getResourceAsStream() work?" Watch "How does getClassLoader().getResourceAsStream() work?" New topic
Author

How does getClassLoader().getResourceAsStream() work?

Deepti Vashisht
Greenhorn

Joined: Jun 15, 2009
Posts: 3
Hi,

I have the following structure of the ear:
test.ear
----META-INF
----test.war
----------META-INF
----------WEB-INF
-----------lib

Inside the lib, I have all the relevant jar files needed for the application. Now in the lib folder, I have 2 jar files (a.jar and b.jar) each containing a property file of the same name.

I need to read the contents of the file in b.jar.

I use
TestImpl.class.getClassLoader().getResourceAsStream(filename), where filename is the name of the property file.

TestImpl.class exists in a.jar.

Whenever I execute this, the property file in a.jar is read instead of the one in b.jar.

The same works fine in another ear structure as under:

test1.ear
----META-INF
----all relevant wars and jar files

Here a.jar and d.jar inside the ear contain the property file of the same name.
When TestImpl.class.getClassLoader().getResourceAsStream(filename), where TestImpl.class is part of a.jar is executed, then the property file in d.jar is read.

I am not sure how it works in the second case? The filename does not contain the /, therefore it goes to the root of the jar file and picks up the property file.

But how does it gives preference to the jar file. I think it is based on the classpath but then i have no control over how the jar files are loaded at runtime by the application server.

How do I solve this problem withoput writing new code and using the same piece of code in both scenarios and have the correct result.

Thanks.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18887
    
    8

Deepti Vashisht wrote:I think it is based on the classpath but then i have no control over how the jar files are loaded at runtime by the application server.


Yes, that's a pretty good summary of how it works. Although because Websphere uses a lot of different classloaders, it's a bit more complicated than just "the classpath".

Anyway my suggestion would be to fix your deployment so that you aren't deploying two copies of the resource. Or at least if you must deploy two copies, make sure they contain the same data, so that it doesn't matter which one Websphere chooses.
Deepti Vashisht
Greenhorn

Joined: Jun 15, 2009
Posts: 3
Thanks Paul.

I resolved the issue by making sure that the property file names in the 2 jar files were different.
Deepti Vashisht
Greenhorn

Joined: Jun 15, 2009
Posts: 3
Thanks Paul.

I was able to resolve the issue by having different property file names in the 2 jar files.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How does getClassLoader().getResourceAsStream() work?