问题
I have a problem implementing this logic in my project. Im using ServiceLoader and I don't know or if it's even possible, but I want to dynamically add JARs to some known directory for app, so the app can load them when it starts. The only thing I achieved was that I know how to create extensible ONE JAR (with META-INF/services), so I can add more and more providers and classes (implementations) to this JAR. The JAR is included to classpath like ordinary library, so app knows about it. What I want to achieve: I don't want to extend one jar file, but I want to extend app by adding more jars like plugins with providers (services), but I don't know how.
Thanks for help!
回答1:
You just need (a) a URLClassLoader that is constructed with the URLs of all the JAR files in those directories, and (b) call ServiceLoader.load(Class service, ClassLoader loader).
回答2:
i think you may create a your customer ClassLoader. The ClassLoader will loader class from jars in dynamic mode. Many framework have some function very like what you want. you can look some code in them. the WebAppClassLoader class in jetty may a start point!
来源:https://stackoverflow.com/questions/11447699/java-spi-serviceloader-adding-multiple-jars-to-app-classpath