Can I dynamically unload and reload (other versions of the same) JAR?

前端 未结 6 788
北恋
北恋 2020-12-05 07:40

I am writing a server program which is used to run unit tests of an API (displaying lots of information and providing web access to control / monitor the whole thing)...<

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-05 08:21

    I think if you load a class using

    Class.forName(clsname, init, classloader); 
    

    (Javadoc here) you will get an instance of the class provided by the given classloader. Everything loaded because of that class will also be loaded via the same classloader.

    As long as you're very careful with the objects instantiated from this point on (to allow for GC), you should be able to reload different versions. I did this once before with Java 1.3, it took a lot of debugging, but at the end I had a "bootstrap" application that loaded a Runnable class by name and was able to "soft-restart" by instantiating a new classloader against a different URL and going again.

提交回复
热议问题