如何在运行时加载jar文件
我被要求构建一个 java 系统,该系统能够在运行时加载新代码(扩展)。 如何在代码运行时重新加载 jar 文件? 或者如何加载新罐子?
显然,由于持续的正常运行时间很重要,因此我想添加重新加载现有类的功能(如果它不会使事情变得太复杂的话)。
我应该注意哪些事项? (将其视为两个不同的问题 - 一个关于在运行时重新加载类,另一个关于添加新类)。
I was asked to build a java system that will have the ability to load new code (expansions) while running.
How do I re-load a jar file while my code is running? or how do I load a new jar?
Obviously, since constant up-time is important, I'd like to add the ability to re-load existing classes while at it (if it does not complicate things too much).
What are the things I should look out for?
(think of it as two different questions - one regarding reloading classes at runtime, the other regarding adding new classes).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
用现有数据重新加载现有类可能会破坏事情。
您可以相对轻松地将新代码加载到新的类加载器中:
不再使用的类加载器可以被垃圾收集(除非存在内存泄漏,就像使用 ThreadLocal、JDBC 驱动程序、java.beans 时经常出现的情况一样) > 等)。
如果您想保留对象数据,那么我建议使用持久性机制,例如序列化或您习惯的任何机制。
当然,调试系统可以做更奇特的事情,但更老套且不太可靠。
可以将新类添加到类加载器中。 例如,使用
URLClassLoader.addURL
。 但是,如果一个类无法加载(因为,比如说,您还没有添加它),那么它永远不会在该类加载器实例中加载。Reloading existing classes with existing data is likely to break things.
You can load new code into new class loaders relatively easily:
Class loaders no longer used can be garbage collected (unless there is a memory leak, as is often the case with using ThreadLocal, JDBC drivers,
java.beans
, etc).If you want to keep the object data, then I suggest a persistence mechanism such as Serialisation, or whatever you are used to.
Of course debugging systems can do fancier things, but are more hacky and less reliable.
It is possible to add new classes into a class loader. For instance, using
URLClassLoader.addURL
. However, if a class fails to load (because, say, you haven't added it), then it will never load in that class loader instance.这对我有用:
This works for me:
您可能希望您的系统基于 OSGi(或者至少花了很多时间),它正是针对这种情况而设计的。
弄乱类加载器确实是一件棘手的事情,主要是因为类可见性的工作原理,并且您不希望稍后遇到难以调试的问题。 例如, Class.forName() ,在许多库中广泛使用,但在碎片化的类加载器空间上效果不佳。
You might want to base your system on OSGi (or at least take a lot at it), which was made for exactly this situation.
Messing with classloaders is really tricky business, mostly because of how class visibility works, and you do not want to run into hard-to-debug problems later on. For example, Class.forName(), which is widely used in many libraries does not work too well on a fragmented classloader space.
我用谷歌搜索了一下,在这里找到了这段代码:
任何人都可以分享意见/评论/关于这种方法的答案?
I googled a bit, and found this code here:
Can anyone share opinions/comments/answers regarding this approach?
使用 org.openide.util.Lookup 和 ClassLoader 动态加载 Jar 插件,如下所示。
Use org.openide.util.Lookup and ClassLoader to dynamically load the Jar plugin, as shown here.