Announcement Announcement Module
Collapse
No announcement yet.
strange class loader issue Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • strange class loader issue

    Hi all,

    I am trying to use spring in a small project which uses java reflection.

    I have created a URLClassloader instance from user supplied .jar files and .class files. The example code is something like this.

    Code:
    	public static void main(String[] args) throws Exception {
    // Use user spplied .jar files
    		URL[] urls = new URL[] { new URL("file:spring.jar") };
    		URLClassLoader urlClsLoader = new URLClassLoader(urls);
    		String className = "org.springframework.beans.factory.config.MapFactoryBean";
    		Class cls = Class.forName(className, true, urlClsLoader);
    		System.out.println("Class " + className);
    		System.out.println("\textends->" + cls.getSuperclass().getName());
    	}
    Now, if i add spring.jar in classpath of this project, the output i get is
    Class org.springframework.beans.factory.config.MapFactor yBean
    extends->java.lang.Object
    Which is wrong.

    But if i remove spring.jar from classpath, and then run the application, i get the correct output as
    Class org.springframework.beans.factory.config.MapFactor yBean extends->org.springframework.beans.factory.config.Abstract FactoryBean

    Can anybody clarify on this issue ?

  • #2
    Siddarth,

    This sounds like an issue related to the classloader you are using or the way in which the classloader is created. Spring doesn't do anything special with its classes, nor does it perform any 'fancy' classloading.

    I don't think this is an issue related to Spring.

    Regards,

    Rob

    Comment


    • #3
      strange class loader issue

      Rob,

      I agree that there could be some problem in the way the classloader is instantiated, but point to note here is, the o/p changes only when I add or remove the spring.jar file from classpath.

      This gives problems to all the jars i add in the classloader, and not just to spring jar.

      If i add following code to the previous one, to find the implemented interfaces, it works fine despite of adding or removing the spring.jar file from the classpath.

      Code:
      Class[] interfaces = cls.getInterfaces();
      		System.out.println("Class " + className);
      		for &#40;int i = 0; i < interfaces.length; i++&#41; &#123;
      			System.out.println&#40;"\timplements->" + interfaces&#91;i&#93;.getName&#40;&#41;&#41;;
      		&#125;
      But the getSuperclass returns java.lang.Object.

      javascript:emoticon('')
      Sad

      I am still not able to get the reason behind this issue.

      Comment


      • #4
        You must have another copy of spring.jar on the classpath. If you remove the spring.jar then Spring classes cannot be loaded and you would not see the output you are seeing you would get an exception.

        Rob

        Comment


        • #5
          :oops:

          Sorry ... it was my mistake ... reffering to wrong docs.

          Its working fine now.

          Comment

          Working...
          X