动态重新加载jar文件内容 [英] Reloading jar files contents dynamically

查看:599
本文介绍了动态重新加载jar文件内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序的类路径中有一个jar文件。在运行时,我将新的类添加到jar文件中,有时也修改现有类的字段/方法。目前我正在使用URLClassLoader动态加载类。动态添加的新类正确加载,我可以在运行时使用它们。但是它无法重新加载在运行时修改的现有类。我读了很多文章,指出我们需要明确处理重新加载,因为一旦加载的类将不会被重新加载,直到所有对类的引用都被销毁为止。此外,我尝试了我发现的示例代码,但没有一个工作。



任何人都可以建议我正确的重新加载方法?任何相同的代码将被高度赞赏。

解决方案

通常要重新加载一个类,您需要卸载整个类加载器。即删除对该类加载器加载的所有类的所有引用。



另一个选项是使用工具来更改现有类的字节码。这通常带有限制,变化的领域是你不能做的。即该类型的对象必须以某种方式进行翻译。



我通常所做的是具有启动/重启功能的服务。这样,您可以通过按下我的IDE中的运行轻松地重新启动需要更新代码的进程。这也尽量减少部署时间。


I have one jar file in my application's class path. At run time, I add new classes to the jar file and sometimes also modify the fields/methods of the existing classes. Currently I am using URLClassLoader to load the classes dynamically. The new classes added dynamically are loaded correctly and I am able to use them at runtime. But it fails to reload the existing classes that are modified at runtime. I read many articles which states we need to explicitly handle reloading because class once loaded will not be reloaded until all the references to the class are destroyed. Also I tried out sample code that I found but none of them worked.

Can anyone suggest me a proper approach for reloading ? Any sample code for the same will be highly appreciated.

解决方案

Normally to reload a class you need to unload the entire class loader. i.e. remove all references to all classes loaded for that class loader.

Another option is to use instrumentation to change the byte code of an existing class. This usually comes with limitations and changing fields is something you cannot do. i.e. the objects of that type would have to be translated somehow.

What I normally do is have services which are very quick to start/restart. This way to you easily restart a process which needs updated code ideally by pressing the Run in my IDE. This minimises deployment time as well.

这篇关于动态重新加载jar文件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆