首页 文章

找不到模块化的Java模块

提问于
浏览
1

我阅读了所有的模块化文档,但仍然无法弄清楚问题是什么:我通过Maven添加了一个依赖项(我也试过手动将jar添加到项目库中但仍然导致同样的问题),但是当我从中导入类时特别是在我的 class 中,IntelliJ说“包com.fazecast.jSerialComm在模块'com.fazecast.jSerialComm'中声明,但模块com.greeting不读它” .

它给了我两个选项:将它添加为我已经做过的Maven依赖(它在我的pom.xml中,可以在依赖项下看到它)或"add requires com.fazecast.jSerialComm directive to module-info.java" . 如果我添加 requires com.fazecast.jSerialComm ,它编译得很好,但是当我创建我的模块化jar并尝试使用 java -p mods/ -m com.greeting/com.mayapp.Runner 运行jar时,它告诉我"java.lang.module.FindException: Module com.fazecast.jSerialComm not found, required by com.greeting" .

我也尝试了更多的jar /依赖,它给了我同样的问题 . 我尝试了Java 9和10,也使用了Gradle,并通过IntelliJ创建了工件 . 得到了同样的例外 . 我的 module-info.java 在我的应用程序启动的 src/main/java 中 . 任何帮助将受到高度赞赏 .

Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-17T14:33:14-04:00)
    Maven home: C:\Apache\apache-maven-3.5.4-bin\apache-maven-3.5.4\bin\..
    Java version: 9.0.4, vendor: Oracle Corporation, runtime: C:\Program iles\Java\jdk-9.0.4 Default locale: en_US, platform encoding: Cp1252
    OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
    Intelli J 2018.1.6

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0

    http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.myapp</groupId>
    <artifactId>greet</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>10</source>
                    <target>10</target>
                    <release>9</release>
                    <executable>javac10</executable>
                    <compilerArgs>
                        <arg>-Xlint:all,-processing</arg>
                    </compilerArgs>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>com.fazecast</groupId>
            <artifactId>jSerialComm</artifactId>
            <version>2.0.2</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
</project>


    package com.myapp;

    import com.fazecast.jSerialComm.SerialPort;
    public class Runner {
     public static void main(String[] args) {
        System.out.println("Runner");
     }
    }


    module com.greeting {
    requires com.fazecast.jSerialComm;
    }

1 回答

  • 0

    如果您开发了一个模块(如 module-info.java 所示),all dependencies need to be required(这是IntelliJ建议的快速修复),需要on the module path(而不是类路径)进行编译和启动 . 对于编译,Maven,Gradle和IntelliJ做正确的事情,当从IntelliJ启动你的应用程序时,它也会这样做 .

    问题很可能在于你的 java 命令:

    java -p mods/ -m com.greeting/com.mayapp.Runner
    

    它指向 mods 作为模块路径,但除非您忘记提及您执行的某些步骤,否则该文件夹不包含您的应用程序的依赖项,因此当Java寻找它们时,它将找不到它们 - 因此错误消息 .

    要将依赖项放入该文件夹,您可以手动复制它们(不要忘记传递依赖项)或使用Maven's copy-dependencies mojo

    <project>
        [...]
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <version>3.1.1</version>
                    <executions>
                        <execution>
                            <id>copy-dependencies</id>
                            <phase>package</phase>
                            <goals>
                                <goal>copy-dependencies</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>${project.build.directory}/../mods</outputDirectory>
                                <overWriteReleases>false</overWriteReleases>
                                <overWriteSnapshots>false</overWriteSnapshots>
                                <overWriteIfNewer>true</overWriteIfNewer>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
        [...]
    </project>
    

    当您运行 mvn packagemvn install 时,您将在 mods 中找到所有依赖项,包括可传递的依赖项,然后您的命令应该可以正常工作 .

相关问题