简体   繁体   English

ServiceLoader不加载实现

[英]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: 我尝试实现一个ServiceLoader,因此创建了一个示例类:

项目结构

the code is simple: 代码很简单:

testInterface.java testInterface.java

package com.test;

public interface testInterface {
    void test();
}

testImpl.java testImpl.java

package com.test;

public class testImpl implements testInterface {

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

} 

Main.java 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.testInterface

com.test.testImpl

I keep getting a NoSuchElementException at the iterator part which means that the implementation was not loaded. 我一直在迭代器部分得到NoSuchElementException,这意味着没有加载实现。 Thanks in advance. 提前致谢。

Put your META-INF/services/ into resources/ and add it to the Eclipse project as a source folder. 将META-INF / services /放入资源/并将其作为源文件夹添加到Eclipse项目中。 It will be automatically included in the JAR file when you compile. 编译时它将自动包含在JAR文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM