Create new ClassLoader to reload Class

前端 未结 2 1727
忘了有多久
忘了有多久 2020-12-14 10:35

I want to create a new ClassLoader everytime my method is called.

So I can reload a class without exiting my program.

A way how I c

相关标签:
2条回答
  • 2020-12-14 11:14

    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.

    Quote:

    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");
    
    0 讨论(0)
  • 2020-12-14 11:17

    saw this ? ClassLoader Load / Reload Example

    I think this blog can satisfy your requirement.

    0 讨论(0)
提交回复
热议问题