You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Michael Anstis <mi...@gmail.com> on 2011/04/23 00:00:57 UTC

Tomcat 6: Classloading: META-INF\services: Nested JARs

Hi,

I am trying to resolve a problem that I assume relates to class loading.

My application contains a JAR that contains a file in META-INF\services:-

MyApp
  \-- WEB-INF
    \-- lib
      \-- AJar.jar
        \-- META-INF
          \-- services
            \-- Afile

MyApp tries to load Afile using:-

ClassLoader.getSystemResources("META-INF/services/AFile)

However the file is not found.

I have read the documentation relating to classloading and META-INF is not
shown as a path searched.

Am I doing something stupid? Should this work?

Thanks,

Mike

Re: Tomcat 6: Classloading: META-INF\services: Nested JARs

Posted by Pid <pi...@pidster.com>.
On 4/22/11 11:00 PM, Michael Anstis wrote:
> Hi,
> 
> I am trying to resolve a problem that I assume relates to class loading.
> 
> My application contains a JAR that contains a file in META-INF\services:-
> 
> MyApp
>   \-- WEB-INF
>     \-- lib
>       \-- AJar.jar
>         \-- META-INF
>           \-- services
>             \-- Afile
> 
> MyApp tries to load Afile using:-
> 
> ClassLoader.getSystemResources("META-INF/services/AFile)

Typically the way that services are used:
    \-- my.jar
     \-- META-INF
      \-- services
       \-- org.package.SomeInterface

Containing:

  org.package.impls.SomeInterfaceImpl

which is a class requiring a no arg constructor.
Then, in your code:

  import java.util.ServiceLoader;

  ... etc ...

  ServiceLoader<SomeInterface> loader =
    ServiceLoader.load(SomeInterface.class);

  for (SomeInterface si : loader) {
     // use implementation		
  }


p


Re: Tomcat 6: Classloading: META-INF\services: Nested JARs

Posted by Pid <pi...@pidster.com>.
On 4/27/11 12:46 AM, Pid wrote:
> On 4/23/11 9:47 AM, Michael Anstis wrote:
>> Hey, no problem. I deserved it.
>>
>> I'm happy your answer fixed my problem :)
>>
>>
>> On 23 April 2011 01:09, Caldarale, Charles R <Ch...@unisys.com>wrote:
>>
>>>> From: Michael Anstis [mailto:michael.anstis@gmail.com]
>>>> Subject: Re: Tomcat 6: Classloading: META-INF\services: Nested JARs
>>>
>>>> Thanks for the candid reply Chuck.
>>>
>>> Sorry to have spoken that way.  Couldn't resist answering the question
>>> given the way it was phrased.
>>>
>>>  - Chuck
> 
> Is it me, or was this thread hijacked 3 times?

No it was me.  (Or Gmail).  Weirdness now removed.


p


Re: Tomcat 6: Classloading: META-INF\services: Nested JARs

Posted by Pid <pi...@pidster.com>.
On 4/23/11 9:47 AM, Michael Anstis wrote:
> Hey, no problem. I deserved it.
> 
> I'm happy your answer fixed my problem :)
> 
> 
> On 23 April 2011 01:09, Caldarale, Charles R <Ch...@unisys.com>wrote:
> 
>>> From: Michael Anstis [mailto:michael.anstis@gmail.com]
>>> Subject: Re: Tomcat 6: Classloading: META-INF\services: Nested JARs
>>
>>> Thanks for the candid reply Chuck.
>>
>> Sorry to have spoken that way.  Couldn't resist answering the question
>> given the way it was phrased.
>>
>>  - Chuck

Is it me, or was this thread hijacked 3 times?


p


Re: Tomcat 6: Classloading: META-INF\services: Nested JARs

Posted by Michael Anstis <mi...@gmail.com>.
Hey, no problem. I deserved it.

I'm happy your answer fixed my problem :)


On 23 April 2011 01:09, Caldarale, Charles R <Ch...@unisys.com>wrote:

> > From: Michael Anstis [mailto:michael.anstis@gmail.com]
> > Subject: Re: Tomcat 6: Classloading: META-INF\services: Nested JARs
>
> > Thanks for the candid reply Chuck.
>
> Sorry to have spoken that way.  Couldn't resist answering the question
> given the way it was phrased.
>
>  - Chuck
>
>
> THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
> MATERIAL and is thus for use only by the intended recipient. If you received
> this in error, please contact the sender and delete the e-mail and its
> attachments from all computers.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

RE: Tomcat 6: Classloading: META-INF\services: Nested JARs

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Michael Anstis [mailto:michael.anstis@gmail.com] 
> Subject: Re: Tomcat 6: Classloading: META-INF\services: Nested JARs

> Thanks for the candid reply Chuck.

Sorry to have spoken that way.  Couldn't resist answering the question given the way it was phrased.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Tomcat 6: Classloading: META-INF\services: Nested JARs

Posted by Michael Anstis <mi...@gmail.com>.
Thanks for the candid reply Chuck.

I feel a bit stupid now, but one learns from their mistakes.

With kind regards,

Mike

On 22 April 2011 23:19, Caldarale, Charles R <Ch...@unisys.com>wrote:

> > From: Michael Anstis [mailto:michael.anstis@gmail.com]
> > Subject: Tomcat 6: Classloading: META-INF\services: Nested JARs
>
> > ClassLoader.getSystemResources("META-INF/services/AFile)
>
> We'll assume that the missing closing quotation mark is a typo.
>
> > However the file is not found.
>
> Nor should it be; let's read the API spec:
>
> getSystemResources
>
> public static Enumeration<URL> getSystemResources(String name)
>                                           throws IOException
>
>    Finds all resources of the specified name from the search path used to
> load classes. The resources thus found are returned as an Enumeration of URL
> objects.
>
>    The search order is described in the documentation for
> getSystemResource(String).
>
> getSystemResource
>
> public static URL getSystemResource(String name)
>
>    Find a resource of the specified name from the search path used to load
> classes. This method locates the resource through the system class loader
> (see getSystemClassLoader()).
>
> Note that the getSystemResources() method is *static*, so it's unrelated to
> the your webapp's classloader.  This is further clarified by the doc for
> getSystemResource(), which explicitly says it uses only the system class
> loader (hence the use of the word "system" in the method names).
>
> > Am I doing something stupid? Should this work?
>
> Yes. No.
>
> You need to use an instance method of your webapp's classloader;
> getResourceAsStream() is usually recommended, since that keeps the operation
> independent of the file system (even in cases where there isn't one, such as
> inside a JAR).  Thread.currentThread().getContextClassLoader() is a way to
> get a reference to your current classloader, assuming you're running inside
> a servlet.
>
>  - Chuck
>
>
> THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
> MATERIAL and is thus for use only by the intended recipient. If you received
> this in error, please contact the sender and delete the e-mail and its
> attachments from all computers.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

RE: Tomcat 6: Classloading: META-INF\services: Nested JARs

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Michael Anstis [mailto:michael.anstis@gmail.com] 
> Subject: Tomcat 6: Classloading: META-INF\services: Nested JARs

> ClassLoader.getSystemResources("META-INF/services/AFile)

We'll assume that the missing closing quotation mark is a typo.

> However the file is not found.

Nor should it be; let's read the API spec:

getSystemResources

public static Enumeration<URL> getSystemResources(String name)
                                           throws IOException

    Finds all resources of the specified name from the search path used to load classes. The resources thus found are returned as an Enumeration of URL objects.

    The search order is described in the documentation for getSystemResource(String).

getSystemResource

public static URL getSystemResource(String name)

    Find a resource of the specified name from the search path used to load classes. This method locates the resource through the system class loader (see getSystemClassLoader()).

Note that the getSystemResources() method is *static*, so it's unrelated to the your webapp's classloader.  This is further clarified by the doc for getSystemResource(), which explicitly says it uses only the system class loader (hence the use of the word "system" in the method names).

> Am I doing something stupid? Should this work?

Yes. No.

You need to use an instance method of your webapp's classloader; getResourceAsStream() is usually recommended, since that keeps the operation independent of the file system (even in cases where there isn't one, such as inside a JAR).  Thread.currentThread().getContextClassLoader() is a way to get a reference to your current classloader, assuming you're running inside a servlet.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org