commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simon Kitching <skitch...@apache.org>
Subject [general] ClassLoader question
Date Thu, 02 Mar 2006 09:43:26 GMT
Hi y'all

I'm puzzled by the behaviour of the Class.getResourceAsStream vs
ClassLoader.getResourceAsStream methods.

I'm trying to implement the recommended "java service provider" pattern,
which involves looking for a file with the name of an API class
in /META-INF/services, but I can't seem to get the file when using a
specific ClassLoader.

Can anyone suggest why the following code might print this?
  Found via class
  Not found via classloader

==== code ====

InputStream s1 = LogTest.class.getResourceAsStream(
  "/META-INF/services/org.apache.commons.logging.LogFactory");
if (s1 == null) {
  System.out.println("Not found via class");
} else {
  System.out.println("Found via class");
  s1.close();
}

InputStream s2 = LogTest.class.getClassLoader().getResourceAsStream(
  "/META-INF/services/org.apache.commons.logging.LogFactory");
if (s2 == null) {
  System.out.println("Not found via classloader");
} else {
  System.out.println("Found via classloader");
  s2.close();
}


I've tried sun java 1.4.2 and 1.5.0_04 and got the same results.

Thanks,

Simon


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message