Search code examples
javajarclassloader

Dynamically load a jar


My application needs some components (jar files) to be loaded/unloaded dynamically on request. For example, I have abc.jar and xyz.jar and on demand my application should be able to load/unload abc.jar or xyz.jar or replace these jars with different versions of jars. Are there any approaches to load/unload jar files dynamically (i.e. without restarting the server)?

While reading in net I got few ideas about osgi, but I don't want to go into framework which may make complex. Is there are simple approach to achieve this? I am using Java, J2EE and Jetty 6 server.


Solution

  • Have a look at the seminal paper "Dynamic class loading in the Java virtual machine", by Bracha and Liang. They explain how class loaders work, and how you can use interfaces and class loader to support dynamic changes. It's a worthy read (you should be able to obtain a free PDF by googling the name of the paper) !