Service Loader Can't Find Provider

463 Views Asked by At

I am having trouble getting the service loader to find my providers. My set up is as follows:

I have 4 modules:

  • EntryPoint
  • Library
  • Version1
  • Version2

In Library, I have an interface defined:

interface Parent{
    fun doSomething()
    fun getName()
} 

In Version1 and Version2, I override that interface with a class called Impl. I also place

META-INF/services/parent.package.path.Parent

in the resources folder, with

 child.package.path.Impl

as its contents.

In EntryPoint I have a main function with the following code:

fun main(args<String>){
    val impls = ServiceLoader.load(Parent::class.java)
    val implToUse = impls.find { i -> i.getName() == args[0]}
    implToUse!!.doSomething()
}

The idea is to be able to load the implementation classes from the entry point dynamically to cut down on build times. everything compiles, but when I run the main function, I get a null pointer exception because none of the providers were loaded.

Not sure what else is required to get the ServiceLoader to "see" the providers. A few notes:

  • in the build folder generated during compilation, the META-INF resource does (in fact) appear in resouces.
  • in the build folder generated during compilation, there is no jar being created. Not sure if that is what is causing the issue, or if it is, how to fix
0

There are 0 best solutions below