3 Replies Latest reply on Mar 20, 2013 11:25 AM by sven.plath

    JBoss 7 application loads resource from dependency, not from itself

    sven.plath

      Hey,

       

      I have yet another thing I would like to share with you guys:

       

      Consider the following example:

      • deployment unit foo with resource.properties in the root of the jar.
      • deployment unit bar with resource.properties in the root of the jar.
      • foo depends on bar.

       

      Now, when trying to obtain the resource.properties using the following code:

       

      getClass().getResourceAsStream("/resource.properties");
      

       

      I always get the resource.properties of the dependency bar, not the resource.properties in the foo.jar.

       

      I also tried using getClass().getClassLoader() and all other available methods, behavior is the same all the time.

       

      Note that in JBoss 6, it is working as intended.

       

      Regards,

      Sven

        • 1. Re: JBoss 7 application loads resource from dependency, not from itself
          jaikiran

          With that dependency, you have made available more than one resource with the same name in the classpath of that application. As a result, the classloader.getResource() returns the first one it finds in the classpath (note, there's a different classloader.getResources() method which returns more than one such resources which are found in the classpath).

           

          I am not sure what the classloader policy is in JBoss Modules when it scans for resources (i.e. whether it checks for dependencies first or itself).

          • 2. Re: JBoss 7 application loads resource from dependency, not from itself
            sven.plath

            Hey,

             

            yes you are right. When using the getClassloader().getResources() method, it returns two different resource.properties URLs. This clarifies everything, thanks a lot.

             

            Regards,

            Sven

            • 3. Re: JBoss 7 application loads resource from dependency, not from itself
              sven.plath

              I would like to add that this behavior is, from my point of view not very intuitive. I would always guess that even when there are multiple resources with the same name in the same location in different deployment units, where one depends on the other, the resource from the dependent is taken.

               

              The JBoss 6 does also find two resources when using getClassloader().getResources(), however the behaivor is as expected, the resource from the dependent is returned.