我可以动态卸载和重新加载(相同的其他版本)JAR吗? [英] Can I dynamically unload and reload (other versions of the same) JAR?

查看:103
本文介绍了我可以动态卸载和重新加载(相同的其他版本)JAR吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个服务器程序,用于运行API
的单元测试(显示大量信息并提供Web访问以控制
/监控整个事情)...



此API在编译期间服务器已知 ,并以
作为JAR提供。



为了能够比较API的不同版本
的单元测试结果(无需重新启动服务器),
I希望能够卸载API的当前版本,
并重新加载较新版本(或旧版本)。



我不喜欢我想使用URLClassLoader并通过名称调用每个
方法

(使用 getDeclaredMethod(someMethod) ),

,因为服务器在很大程度上取决于API,并且很难以这种脏的方式包装每个方法调用。$ / b

我在想:由于J的所有接口都是所有版本AR
相同,我不能通过某种方式重新加载JAR的其他版本
(没有那个by-name-invokation?)。



注意:我使用的是最新的Java SE(6)和Java EE(5)。



如果您认为,我想要达到的目标是不可能的,
请建议解决方法或不同的概念。

解决方案

我认为如果你使用

加载一个类

  Class.forName(clsname,init,classloader); 

Javadoc 这里你会得到一个实例由给定的类加载器提供的类。由于该类加载的所有内容也将通过相同的类加载器加载。



只要你对从这一点开始实例化的对象非常小心(允许GC),您应该可以重新加载不同的版本。我之前用Java 1.3做了一次,它需要大量的调试,但最后我有一个bootstrap应用程序按名称加载 Runnable 类并且能够通过针对不同的URL实例化新的类加载器并再次进行软重启。


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)...

This API is known to the server during compile time and is provided as a JAR.

To be able to compare between unit test results of different versions of the API (without restarting the server), I want to be able to unload the 'current' version of the API, and to reload a newer one (or an older one).

I don't want to use URLClassLoader and invoke every single method by name
( using getDeclaredMethod("someMethod") ),
because the server heavily depends on the API and it would be complicated to 'wrap' every method call in such dirty way.

I was thinking: Since all interfaces of all versions of the JAR are same, couldn't I do it by somehow reloading an other version of the JAR (without that by-name-invokation?).

Note: I am using latest Java SE (6) and Java EE (5).

If you think, what I'm trying to achieve is not possible, please suggest a 'workaround' or a different concept.

解决方案

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.

这篇关于我可以动态卸载和重新加载(相同的其他版本)JAR吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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