ServiceLoader does not load implementation

I really researched a lot before asking about it, it seems like I'm missing something. I am trying to implement ServiceLoader and therefore made an example class:

Project structure

the code is simple:

testInterface.java

package com.test;

public interface testInterface {
    void test();
}

testImpl.java

package com.test;

public class testImpl implements testInterface {

    @Override
    public void test() {
        System.out.println("test");
    }

} 

Main.java

package com.test;

import java.util.ServiceLoader;

public class Main {

    public static void main(String[] args) {
        ServiceLoader<testInterface> serviceLoader = ServiceLoader.load(testInterface.class);

        serviceLoader.iterator().next().test();
    }

}

com.test.testInterface

com.test.testImpl

I keep getting a NoSuchElementException on the iterator part, which means the implementation has not been loaded. Thanks in advance.

+4
source share
1 answer

Put your META-INF / services / in resources / and add it to your Eclipse project as the source folder. It will be automatically included in the JAR file at compilation.

+3
source

Source: https://habr.com/ru/post/1613737/


All Articles