1

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
cershif
  • 154
  • 1
  • 13
  • Did you try to print `impls` to see if `Impl` is there? Are `Version1` and `Version2` modules dependencies of `EntryPoint`? Recheck if all files are in correct directories, all package names are correct, etc. Could you publish the full source code? – broot Jun 20 '22 at 17:15
  • @broot all package names are correct (intellij autocompletes, and yells at me if I try to change to something incorrect). `Version1` and `Version2` are runtime dependencies of `EntryPoint` (but I get the same error if they are implementation dependencies). I tried to loop through `impls` and it never enters the loop (meaning it is not finding the providers). I'll look into publishing the full source, but it really isn't much more than I outlined above. – cershif Jun 20 '22 at 17:24
  • You can also try to build the application into jar files and run it from them. From my experience, we sometimes run into this kind of problems when running the code directly by IntelliJ. I believe this is because during the development resources and class files are in separate directories and in resulting jar they are next to each other. – broot Jun 20 '22 at 17:36
  • I can try that, but I've had this work before, so really want to keep this flow if possible – cershif Jun 20 '22 at 17:37

0 Answers0