Java services loaded with service loader loading their own services with service loader

StackOverflow https://stackoverflow.com/questions/21724526

  •  10-10-2022
  •  | 
  •  

質問

My structure is composed of a main application that loads plug-ins using the Java util service loader. After it loads the service provider JARs, the service providers of the main application tries to load their own service providers. However they cannot accomplish this because the class loader context is that of the main application. Meaning the class loader has no knowledge about interfaces used by the service providers trying to load their own service providers causing a class not found exception. Can anybody suggest how my service providers can load their own service providers using service loader? Is there a way to dynamically change the class loader context during run time? I will try to post some code later. I am currently away from my laptop where the code is located. Thank you.

役に立ちましたか?

解決 2

Well I got it to work using the overloaded method as mentioned above. The only difference is I used load(Class service, Plugin.class.getClassLoader() );. Thread.currentThread().getContextClassLoader() Still returns the main application's class loader

他のヒント

I assume you use ServiceLoader.load(Class service) to load the plugins? If yes, it uses the classloader of the current thread (Thread.currentThread().getContextClassLoader()).

So try to use the overloaded version of this method where you can inject a different classloader:

ServiceLoader<S> load(Class<S> service, ClassLoader loader)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top