This discussion is archived
3 Replies Latest reply: Dec 23, 2009 12:16 AM by 843798 RSS

dynamic method invocation/reflection

843798 Newbie
Currently Being Moderated
I am fairly new to java and got thrown into some fairly entailed stuff dealing with dynamic class loading. Here is my general dilemma.

I have to load a class from a known jar. I know the name fo the class and the jar...it is passed to me from some other component. I make the following calls...

URLClassLoader urlLoader1 = new URLClassLoader(new URL[] { new URL("file", null, thierJar) });
Class<?> c = urlLoader1.loadClass("rtest");
myInterfaces.idbinterface thierInstance = (myInterfaces.idbinterface) c.newInstance();

So far...so good. Now the class has a method that obeys the agreed upon interface...

thierInstance.doit();

the problem is the doit() method also has some classes that it loads..or tries to load and that's when I get the java.lang.NoClassDefFoundError. I also have the list of additional jars that contain the classes but I don't really know which classes I need to load becuase I didn't write the "rtest" class nor do i really know the contents of it...only that it requires classes out of it's own jar possibly as well as the others I have been given. It seems to me that this is no different than what the JVM probably does. It gets an entry point and then resolves all the other references as necessary from the jars it has available.

So, how do I do this dynamically...how can I supply the list of jars to use to resolve the additional references etc.?
Also, it's possible that some of the jars I have been given are the same as jars that my app needs. e.g. they may specify jdom and my app uses it. What is the proper way to handle this situation?

thanks
  • 1. Re: dynamic method invocation/reflection
    EJP Guru
    Currently Being Moderated
    I also have the list of additional jars that contain the classes
    That's all you need. Just add URLs for them to the array of URLs you create when constructing the class loader.
  • 2. Re: dynamic method invocation/reflection
    jtahlborn Expert
    Currently Being Moderated
    mccorb101 wrote:
    Also, it's possible that some of the jars I have been given are the same as jars that my app needs. e.g. they may specify jdom and my app uses it. What is the proper way to handle this situation?
    in general, you don't need to do anything. java's class loading mechanism handles all this. classloaders chain together with parent/child references. most normal classloader implementations attempt to load a class from the parent before attempting to load it in the child. so, if you have a jdom jar available to both the parent and child classloader, it will only ever be loaded in the parent (and any classes in the child classloader will use these classes as well). you don't have to do anything.

    the only time this doesn't work, is if the 2 jars are different versions which are not binary compatible. then you have problems. hopefully, you won't have to deal with this. :)
  • 3. Re: dynamic method invocation/reflection
    843798 Newbie
    Currently Being Moderated
    yes i agree with jtahlborn