You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Simon Kitching <sk...@apache.org> on 2006/03/02 10:43:26 UTC

[general] ClassLoader question

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


Re: [general] ClassLoader question

Posted by Simon Kitching <sk...@apache.org>.
Never mind, I've found it.

Class.getResourceAsStream requires a slash on the front of the path,
while ClassLoader.getResourceAsStream must NOT have it.

Sigh. 

Sorry for bothering you all.

Cheers,

Simon

On Thu, 2006-03-02 at 22:43 +1300, Simon Kitching wrote:
> 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
> 


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


Re: [general] ClassLoader question

Posted by Craig McClanahan <cr...@apache.org>.
On 3/2/06, Simon Kitching <sk...@apache.org> wrote:
>
> 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


For some wierd reason,  the two approaches are inconsistent with whether you
should put the leading slash on the path or not ... try it without on the
classloader version.

Craig

==== 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
>
>

Re: [general] ClassLoader question

Posted by Niall Pemberton <ni...@blueyonder.co.uk>.
The javadoc indicates that the Class's getResourceAsStream() method converts
"." to "/" - whereas the ClassLoader doesn't.

Niall

----- Original Message ----- 
From: "Simon Kitching" <sk...@apache.org>
To: <co...@jakarta.apache.org>
Sent: Thursday, March 02, 2006 9:43 AM
Subject: [general] ClassLoader question


> 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
>
>
>



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