How do I create a ClassLoader that will not search the parent for loading classes

后端 未结 1 607
面向向阳花
面向向阳花 2021-01-02 17:20

I think I understand how class-loading hierarchies work. (the JVM looks into the parent hierarchy first)

So I would like to create a ClassLoader, or use an existing

相关标签:
1条回答
  • 2021-01-02 18:16

    Simply use the URLClassLoader and supply null as the parent.

    File myDir = new File("/some/directory/");
    ClassLoader loader = null;
    try {
        URL url = myDir.toURL();         
        URL[] urls = new URL[]{url};
        loader = new URLClassLoader(urls, null);
    } 
    catch (MalformedURLException e) 
    {
        // oops
    }
    
    0 讨论(0)
提交回复
热议问题