How to put custom ClassLoader to use?

前端 未结 1 360
孤城傲影
孤城傲影 2020-12-08 20:08

Hello all and thanks for the attention! I have a problem that must both be easy and obvious, yet I am stuck.

I want to deliver dynamically created Java classes to be

相关标签:
1条回答
  • 2020-12-08 20:22

    Not sure I understand the question, you have a 3rd party lib and you want it to use your classloader to load classes.

    If you're lucky the third party lib uses the threads context classloader which you can set using Thread.currentThread().setContextClassLoader(myClassLoader), in the same thread you can access this classloader with Thread.currentThread().getContextClassLoader()...

    Another point, but not sure it is important in your context, is that you can also write a parent-last classloader that will try to load the class before delegating to its parent (instead of trying to delegate first)

    Edited after your comment:

    parent_last classloader will make a difference if your library doesn't rely on the thread context classloader, then you have to load the library with your parent-last classloader thus setting your classloader as the classloader for the library instead of its parent loader (the parent of your classloader)...

    You may also make a classloader with a parent-first behavior but for your 3rd party library...

    And a good link about classloaders...

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