Custom Java classloader not being used to load dependencies?

后端 未结 5 1186
逝去的感伤
逝去的感伤 2021-02-05 10:28

I\'ve been trying to set up a custom classloader that intercepts classes to print out which classes are being loaded into the application. The classloader looks like this

<
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-05 11:02

    If you do

        System.out.println( p.getClass().getClassLoader() );
    

    you'll see that p's classloader isn't your MyClassLoader bcl. It was actually loaded by bcl's parent, the system class loader.

    When PythonInterpreter loads its dependent classes, it'll use its actual class loader, the system classloader, not your bcl, so your interception isn't reached.

    To solve the problem, your classloader can't delegate to its parent, it has to actually load the classes by itself.

    For that you can subclass URLClassLoader (steal the URLs from the system classloader).

提交回复
热议问题