You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@felix.apache.org by Ma...@sophia.inria.fr on 2010/04/15 12:26:21 UTC

ClassLoader.getSystemResourse() behaviour

    I have a problem which I'm not even sure how to start explaining. I'm porting an application called hop to felix. this application is running fine stand alone (e.g., java -jar 
hop.jar), but within felix I have problems with the mentioned method.

    hop uses ClassLoader.getSystemResourse() for testing existence of and loading files from the .jar archive which are not java classes. for instance:

   public static boolean exists( String name ) {
      return java.lang.ClassLoader.getSystemResource( name ) != null;
   }

    as I said this works fine while standalone, but not under felix. I even changed that to:

   public static boolean exists( String name ) {
      return foreign.class.getClassLoader().getSystemResource( name ) != null;
   }

    where foreign is a class already loaded. I'm not sure if felix uses it's own ClassLoader or not, so that code aims to catch that possibility. at the same time, I'm not a java 
expert, so probably this does not address properly the problem. in any case, this change doesn't work either, and I'm not sure how to continue. 

-- 
Lic. Marcos Dione
Engineer Expert - Hop Project
http://hop.inria.fr/
INRIA Sophia Antipolis - Méditerranée
Phone: +33 (0)4 92 38 79 67

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


Re: ClassLoader.getSystemResourse() behaviour

Posted by Christopher Brind <ch...@googlemail.com>.
A few crudely put points might help you understand :)

- getSystemResource is a static method, so you're calling the same thing
regardless of what instance of ClassLoader you call it on.  Convention says
you shouldn't call static methods on an instance and some IDEs, e.g. Eclipse
will probably warn you of this.  Consider what would happen if you called a
static method on an instance variable that is null.

- OSGi bundles have their own classloader.  The framework wires bundles
together such that the classloader of a bundle will only be able to see
classes from packages it imports (this is the mechanism that allows
different versions of the same package to exist in a runtime environment)

- using getSystemResource uses the system (top level) classloader try and
load a resource, which will won't work because system classloader is unaware
of the OSGi bundle class loaders

- getResource is an instance method on ClassLoader, so it uses the actual
class loader you are referencing.  Thus, foreign.class.getClassLoader() is
the class loader of the bundle that the foreign class lives in.

Hope that helps.

Cheers,
Chris



2010/4/15 <Ma...@sophia.inria.fr>

> On Thursday 15 April 2010 12:28:25 Karl Pauls wrote:
> > foreign.class.getClassLoader().getResource(name)
>
>     works perfectly, thanks! can you please explain the difference? I find
> it
> not clear from the documentation.
>
> --
> Lic. Marcos Dione
> Engineer Expert - Hop Project
> http://hop.inria.fr/
> INRIA Sophia Antipolis - Méditerranée
> Phone: +33 (0)4 92 38 79 67
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
> For additional commands, e-mail: users-help@felix.apache.org
>
>

Re: ClassLoader.getSystemResourse() behaviour

Posted by Ma...@sophia.inria.fr.
On Thursday 15 April 2010 12:28:25 Karl Pauls wrote:
> foreign.class.getClassLoader().getResource(name)

    works perfectly, thanks! can you please explain the difference? I find it 
not clear from the documentation.

-- 
Lic. Marcos Dione
Engineer Expert - Hop Project
http://hop.inria.fr/
INRIA Sophia Antipolis - Méditerranée
Phone: +33 (0)4 92 38 79 67

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


Re: ClassLoader.getSystemResourse() behaviour

Posted by Karl Pauls <ka...@gmail.com>.
try:

foreign.class.getClassLoader().getResource(name)

regards,

Karl

2010/4/15  <Ma...@sophia.inria.fr>:
>
>    I have a problem which I'm not even sure how to start explaining. I'm porting an application called hop to felix. this application is running fine stand alone (e.g., java -jar
> hop.jar), but within felix I have problems with the mentioned method.
>
>    hop uses ClassLoader.getSystemResourse() for testing existence of and loading files from the .jar archive which are not java classes. for instance:
>
>   public static boolean exists( String name ) {
>      return java.lang.ClassLoader.getSystemResource( name ) != null;
>   }
>
>    as I said this works fine while standalone, but not under felix. I even changed that to:
>
>   public static boolean exists( String name ) {
>      return foreign.class.getClassLoader().getSystemResource( name ) != null;
>   }
>
>    where foreign is a class already loaded. I'm not sure if felix uses it's own ClassLoader or not, so that code aims to catch that possibility. at the same time, I'm not a java
> expert, so probably this does not address properly the problem. in any case, this change doesn't work either, and I'm not sure how to continue.
>
> --
> Lic. Marcos Dione
> Engineer Expert - Hop Project
> http://hop.inria.fr/
> INRIA Sophia Antipolis - Méditerranée
> Phone: +33 (0)4 92 38 79 67
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
> For additional commands, e-mail: users-help@felix.apache.org
>
>



-- 
Karl Pauls
karlpauls@gmail.com

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