首页 > 解决方案 > Java9 模块 - Maven:ServiceLoader.load(HelloInterface.class) 找不到接口类

问题描述

在 IntelliJ/Maven 中,我想使用 Java9/模块和 ServiceLoader。我构建了一个简单的 2 模块项目。使用 maven ServiceLoader.load( ...) 找不到 HelloInterface.class 的实现。为什么?

我首先添加了 2 个模块,通过 IntelliJ 创建了项目。重建项目工作正常。通过 Maven 清理/安装也可以正常工作 - 好吧,单元测试失败;-(

更新 1:由于将 module-info.java 放在源根目录下,因此可以使用 IntelliJ 运行。我使用“重建项目”进行编译。

如何将这 2 个模块项目与带有 Maven 的 ServiceLoader 一起使用?

我的简单项目结构是:

在此处输入图像描述

pom.xml:

<build>
<plugins>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <source>14</source>
        <target>14</target>
    </configuration>
</plugin>
</plugins>
</build>
<modules>
    <module>java9.com.hello</module>
    <module>java9.com.hello.client</module>
</modules>

文件:模块java9.com.hello/src/main/java/module-info.jar

module java9.com.hello {
    exports com.hello;
    exports com.hello.services;
    provides com.hello.services.HelloInterface with com.hello.services.HelloWorldIntefaceImpl;
}

文件:模块 java9.com.hello / com.hello.services.HelloInterface.java

public interface HelloInterface {
    String sayHello();
}

文件:模块 java9.com.hello / com.hello.services.HelloWorldIntefaceImpl.java

public class HelloWorldIntefaceImpl implements HelloInterface {
    public String sayHello() {
        String helloString = "Hello world by Inteface!";
        System.out.println( helloString);
        return helloString;
    }
}

文件:模块 java9.com.hello / pom.xml(没有样板)

<parent>
    <artifactId>jdk-new-features</artifactId>
    <groupId>org.example</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>java9.com.hello</artifactId>

文件:模块java9.com.hello.client/src/main/java/module-info.jar。该文件给出编译器错误。

module java9.com.hello.client {
    requires java9.com.hello; // <==== gives the ERROR
    uses com.hello.services.HelloInterface;
}

文件:模块 java9.com.hello.client / com.hello.client.HelloWorldClient.java

public class HelloWorldClient {
    public static void main (String arg[]) {
        HelloWorld hello = new HelloWorld();
        System.out.println(hello.sayHelloWorld());
        Iterable<HelloInterface> services = ServiceLoader.load(HelloInterface.class);
        HelloInterface service = services.iterator().next();
        service.sayHello();
    }
    public String callService() {
        Iterable<HelloInterface> services = ServiceLoader.load(HelloInterface.class);
        HelloInterface service = services.iterator().next();
        return service.sayHello();
    }
}

pom.xml是:模块java9.com.hello.client/pom.xml:

<dependencies>
    <dependency>
        <groupId>org.example</groupId>
        <artifactId>java9.com.hello</artifactId>
        <version>1.0-SNAPSHOT</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
</dependencies>

单元测试文件:模块 java9.com.hello.client / com.hello.client.HelloWorldClientTest.java

public class HelloWorldClientTest {
    @Test
    public void testModuleInterfaceImplementation() {
        HelloWorldClient helloWorldClient = new HelloWorldClient();
        assertEquals( "Hello world by Inteface!", helloWorldClient.callService());
    }
}

标签: javamavenjava-9java-module

解决方案


推荐阅读