Swapping a running jar at runtime

試著忘記壹切 提交于 2019-12-09 12:59:41

问题


I am building an update system in which I need to be able to replace a referenced jar of a running application jar at runtime. However, I am running into file locking issues on Windows when trying to perform file utility functions on the jar such as 'setLastModified'.

After some googling I found this snippet...

What I found in my research is that the standard ClassLoader implementation never closes a jar file once it has been opened. It also only loads resources from the jar file as needed. So at any particular time, there may be Classes in the jar file that have not been loaded into memory. Obviously, if you delete the jar file out from under the ClassLoader, and it tries to find a resource in the missing file, you're going to get an IOException at the very least.

Does anyone have any references to information on doing this, or working around this issue?

Thanks.


回答1:


There is a lot of solutions for classes/lib hot-swapping :

  • Hotswap for ant
  • JRebel
  • OSGi

It depends on what you want to do.




回答2:


The closest thing is OSGi

Please have a look at the 'See also' section of the mentioned artice for concrete products.




回答3:


I don't know of any good way to deal with this. Creating a custom classloader might be an option, although I don't like that idea..

We worked around it by running an updater from a separate jar before launching the main application. This works well enough, but obviously, you can only update the jar when the application is launched.



来源:https://stackoverflow.com/questions/3621505/swapping-a-running-jar-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!