javaspringtomcatspring-mvcweb-container

Dynamically loading or replacing jars in a Webcontainer (Tomcat)


My question is actually very simple, though the answer to it may be complex. Is it possible to load jars dynamically at runtime-or even replacing an already loaded one in a webcontainer context? In this specific case I am interested in the combination of Tomcat and Spring MVC but I guess that the technique, if any would be adaptable to other technology stacks.

If the answer to the question is yes, the immediate followup question is: Which is the most straightforward way to do it? I'm aware of OSGi and I'm pretty sure that it is possible with it to load jars dynamically in a webcontainer, but I would consider this a heavyweight solution.


Solution

  • I'm not sure how JARs are loaded exactly but you can dynamically load any class at runtime. If your interest is the classes contained inside a JAR then yes those can be loaded using a class loader. Or more specifically a JAR class loader JarClassLoader Class

    If you are trying to replace a JAR the approach would probably be to replace the classes loaded from that JAR and the only way to do that is if a reference to each of those classes is "garbage collected". At that point you can load the same class from a new location.Class Unloading

    Question similar to yours How Should I load Jars dynamically at run-time?