• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How does getClassLoader().getResourceAsStream() work?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul.

I resolved the issue by making sure that the property file names in the 2 jar files were different.
 
Deepti Vashisht
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul.

I was able to resolve the issue by having different property file names in the 2 jar files.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic