Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@www.apache.org Received: (qmail 46744 invoked from network); 2 Mar 2006 09:54:22 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 2 Mar 2006 09:54:22 -0000 Received: (qmail 59168 invoked by uid 500); 2 Mar 2006 09:55:09 -0000 Delivered-To: apmail-jakarta-commons-dev-archive@jakarta.apache.org Received: (qmail 58625 invoked by uid 500); 2 Mar 2006 09:55:06 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 58614 invoked by uid 99); 2 Mar 2006 09:55:06 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 02 Mar 2006 01:55:06 -0800 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests= X-Spam-Check-By: apache.org Received-SPF: neutral (asf.osuosl.org: local policy) Received: from [210.86.15.147] (HELO mta204-rme.xtra.co.nz) (210.86.15.147) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 02 Mar 2006 01:55:05 -0800 Received: from mta4-rme.xtra.co.nz ([210.86.15.193]) by mta204-rme.xtra.co.nz with ESMTP id <20060302095442.VIAE3844.mta204-rme.xtra.co.nz@mta4-rme.xtra.co.nz> for ; Thu, 2 Mar 2006 22:54:42 +1300 Received: from [10.1.1.6] ([222.153.54.117]) by mta4-rme.xtra.co.nz with ESMTP id <20060302095442.GMIX1416.mta4-rme.xtra.co.nz@[10.1.1.6]> for ; Thu, 2 Mar 2006 22:54:42 +1300 Subject: Re: [general] ClassLoader question From: Simon Kitching Reply-To: skitching@apache.org To: commons-dev@jakarta.apache.org In-Reply-To: <1141292607.3812.67.camel@localhost.localdomain> References: <1141292607.3812.67.camel@localhost.localdomain> Content-Type: text/plain Date: Thu, 02 Mar 2006 22:55:32 +1300 Message-Id: <1141293332.3812.70.camel@localhost.localdomain> Mime-Version: 1.0 X-Mailer: Evolution 2.0.4 Content-Transfer-Encoding: 7bit X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N 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