本文介绍了创建新的ClassLoader重新加载类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个新的的ClassLoader
每次我的方法被调用。
I want to create a new ClassLoader
everytime my method is called.
所以,我可以重新载入类
不退出我的程序。
So I can reload a class
without exiting my program.
一个办法,我怎么可以更新类
按的ClassLoader
加载也将是一个解决方案。
A way how I can update a class
loaded by ClassLoader
would also be a solution.
我怎样才能做到这一点?
How can I achieve that?
推荐答案
我找到了一个很好的解释回答在这里:
I found a good explained answer here:
http://www.exampledepot.com/egs/java.lang/reloadclass.html
重要的是有两个二进制文件夹,在我的情况:
一个用于测试用例,一个用于节目源
The important thing is to have two binary folders, in my case:one for the testcases and one for the program source.
报价:
URL[] urls = null;
try {
// Convert the file object to a URL
File dir = new File(System.getProperty("user.dir")
+File.separator+"dir"+File.separator);
URL url = dir.toURL(); // file:/c:/almanac1.4/examples/
urls = new URL[]{url};
} catch (MalformedURLException e) {
}
try {
// Create a new class loader with the directory
ClassLoader cl = new URLClassLoader(urls);
// Load in the class
Class cls = cl.loadClass("MyReloadableClassImpl");
这篇关于创建新的ClassLoader重新加载类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!