创建新的ClassLoader以重新加载Class

cte*_*ekk 14 java class build

我想在ClassLoader每次调用我的方法时创建一个新的.

所以我可以在class不退出程序的情况下重新加载.

我如何更新class加载的ClassLoader方法也是一种解决方案.

我怎样才能做到这一点?

cte*_*ekk 11

我在这里找到了一个很好的解释答案

http://www.exampledepot.com/egs/java.lang/reloadclass.html

重要的是有两个二进制文件夹,在我的情况下:一个用于测试用例,一个用于程序源.

引用:

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");
Run Code Online (Sandbox Code Playgroud)


lia*_* xu 6

看到了吗? ClassLoader加载/重新加载示例

我认为这个博客可以满足您的要求.