1

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.

nanoquack
  • 949
  • 2
  • 9
  • 26

1 Answers1

1

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?

Community
  • 1
  • 1
Usman Mutawakil
  • 4,993
  • 9
  • 43
  • 80
  • Yes, I'm aware that we can load classes dynamically at runtime, but my question was targeting dynamic class/jar loading inside a web container, eg tomcat. Do you think this would still work inside one? – nanoquack Aug 05 '13 at 19:53
  • Would the "JarClassLoader" Class work when called from inside a web container? I would have to guess yes. It's only purpose is to load resources from Jar files dynamically at runtime. It a distant child class of "ClassLoader" which absolutely works in a web container. – Usman Mutawakil Aug 05 '13 at 20:27
  • To add to the comment I just added above Tomcat uses class loaders itself. If you use the JarClassLoader class or any of the other class loaders it should be easy to test in a few lines of code. – Usman Mutawakil Aug 05 '13 at 20:33