Hr. Burtz
Hr. Burtz

Reputation: 73

ServiceLoader doesn't load implementation

I really did a lot of research before asking this, it seems like I'm missing something. I try to implement a 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 at the iterator part which means that the implementation was not loaded. Thanks in advance.

Upvotes: 7

Views: 6754

Answers (1)

user5500105
user5500105

Reputation: 297

Put your META-INF/services/ into resources/ and add it to the Eclipse project as a source folder. It will be automatically included in the JAR file when you compile.

Upvotes: 10

Related Questions