Solved

Dynamically loading classes from a jar at runtime

Posted on 2010-09-23
7
1,615 Views
Last Modified: 2013-11-24
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
0
Comment
Question by:westerlike
  • 3
  • 3
7 Comments
 
LVL 26

Expert Comment

by:ksivananth
ID: 33751231
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
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33751255
0
 

Author Comment

by:westerlike
ID: 33751289
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?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 40

Expert Comment

by:gurvinder372
ID: 33751333
<<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.
0
 

Author Comment

by:westerlike
ID: 33751378
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.
0
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 500 total points
ID: 33751400
the system classes will not be overriden. duplicate classes are not loaded again. Please read carefully the first link i had posted

to know how to reload a class again, check this
http://www.exampledepot.com/egs/java.lang/reloadclass.html



0
 

Author Closing Comment

by:westerlike
ID: 33844742
Thanks. I managed to create a dynamic loader.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Securing Jmx Console and web console 2 66
GUI builder for Eclipse? 8 27
print map entry 34 55
Way to decrease size of apk file 9 45
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now