Solved

Dynamically loading classes from a jar at runtime

Posted on 2010-09-23
7
1,592 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.

760 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

19 Experts available now in Live!

Get 1:1 Help Now