westerlike
asked on
Dynamically loading classes from a jar at runtime
Hi.
I want to write an application in Java which is able to load JARs from a directory during runtime, add all classes inside to the classpath, load a spring context from the jar and instantiate the main class of it (it implements an Interface). If I add the JAR to the classpath before execution I am able to find the class which implements the interface and load it. I want to do this at runtime and call its functions using reflection (the list of methods will be listed by calling a function that is also noted in the interface). I think I need multiple class loaders and instantiate somehow. Does anyone know ho I can accomplish this?
Thanks,
Sintax
I want to write an application in Java which is able to load JARs from a directory during runtime, add all classes inside to the classpath, load a spring context from the jar and instantiate the main class of it (it implements an Interface). If I add the JAR to the classpath before execution I am able to find the class which implements the interface and load it. I want to do this at runtime and call its functions using reflection (the list of methods will be listed by calling a function that is also noted in the interface). I think I need multiple class loaders and instantiate somehow. Does anyone know ho I can accomplish this?
Thanks,
Sintax
yes as you said you need toi write custom classloaders, take a look http://www.javaworld.com/javaworld/jw-10-1996/jw-10-indepth.html
to understand java's extension mechanism
http://download.oracle.com/javase/tutorial/ext/basics/load.html
to see an example as reference
http://stackoverflow.com/questions/60764/how-should-i-load-jars-dynamically-at-runtime
http://tutorials.jenkov.com/java-reflection/dynamic-class-loading-reloading.html
http://www.exampledepot.com/egs/java.lang/LoadClass.html
http://www.java-tips.org/java-se-tips/java.lang/how-to-load-a-class-that-is-not-on-the-classpath.html
http://download.oracle.com/javase/tutorial/ext/basics/load.html
to see an example as reference
http://stackoverflow.com/questions/60764/how-should-i-load-jars-dynamically-at-runtime
http://tutorials.jenkov.com/java-reflection/dynamic-class-loading-reloading.html
http://www.exampledepot.com/egs/java.lang/LoadClass.html
http://www.java-tips.org/java-se-tips/java.lang/how-to-load-a-class-that-is-not-on-the-classpath.html
ASKER
Thanks, still have some questions though: If I load the jar with a custom loader and call the loadClass of the classloader, will it also load all dependencies of that class (those are also bundled in the jar file)? In my case I have a spring.xml in the jar as well which describes the beans inside, they use several libraries whcih are also bundled inside the jar file. The main entry point class implements and interface that I know and can find it from the context file once the jar is loaded.
How would I approach loading that bean context and instantiating the main class from the jar using the classloader?
How would I approach loading that bean context and instantiating the main class from the jar using the classloader?
<<If I load the jar with a custom loader and call the loadClass of the classloader, will it also load all dependencies of that class (those are also bundled in the jar file)?>>
Yes
<<instantiating the main class from the jar using the classloader?>>
once the class is loaded, you can instantiate it way you normally do.
Yes
<<instantiating the main class from the jar using the classloader?>>
once the class is loaded, you can instantiate it way you normally do.
ASKER
and would the libraries in the jar file interfere with the main classloader versioning/dupe classes? Also would they be able to communicate? I am trying to load the class and have the main class loader call functions using reflection. The jars can be replaced during runtime and the main app would then remove the custom loader and instantiate a new one.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks. I managed to create a dynamic loader.