How to load JAR files dynamically at Runtime?

前端 未结 20 3723
伪装坚强ぢ
伪装坚强ぢ 2020-11-21 05:15

Why is it so hard to do this in Java? If you want to have any kind of module system you need to be able to load JAR files dynamically. I\'m told there\'s a way of doing it b

20条回答
  •  深忆病人
    2020-11-21 06:13

    The solution proposed by jodonnell is good but should be a little bit enhanced. I used this post to develop my application with success.

    Assign the current thread

    Firstly we have to add

    Thread.currentThread().setContextClassLoader(classLoader);
    

    or you will not able to load resource (such as spring/context.xml) stored into the jar.

    Do not include

    your jars into the parent class loader or you will not able to understand who is loading what.

    see also Problem reloading a jar using URLClassLoader

    However, OSGi framework remain the best way.

提交回复
热议问题