首页 文章

找不到Java 9自动模块

提问于
浏览
8

我正在尝试定义Java 9模块 . 我已经定义了类似的东西:

module my.module.name {

}

然后我的许多文件开始给我错误,他们找不到一些包 . 然后我使用了IntelliJ的自动帮助功能,它在我的module-info.java中添加了几个“require”语句 . 所以它变得像:

module my.module.name {
    requires spring.web;
    requires sshd.core;
    requires com.fasterxml.jackson.core;
    ....
}

现在IntelliJ显示我的所有代码没有错误 . 但是当我在“Maven Projects”窗口中单击“compile”时(我正在使用Maven 3.5.3和Oracle JDK 10进行编译),它告诉我以下内容:

src/main/java/module-info.java:[2,20] module not found: spring.web
src/main/java/module-info.java:[11,18] module not found: sshd.core
src/main/java/module-info.java:[13,35] module not found: com.fasterxml.jackson.core
...
same for the other modules I "require".

据我所知,未定义为模块的第三方库会生成“自动”模块 . 所以我发现了我需要的其中一个 jar :

C:\Users\<my username>\.m2\repository\org\springframework\spring-web\5.0.5.RELEASE\spring-web-5.0.5.RELEASE.jar

并希望使用以下命令查看“自动”模块的名称:

jar.exe" --file=spring-web-5.0.5.RELEASE.jar --describe-module

No module descriptor found. Derived automatic module.

spring.web@5.0.5.RELEASE automatic
...

所以根据我的调查,我“需要”的模块的名称是正确的!我错过了什么?我为什么得到

module not found: spring.web

和其他模块一样吗?我错过了一些路径或什么?

Edit :根据要求,这是我的pom.xml:

<?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>mygroupid</groupId>
<artifactId>myartifactid</artifactId>
<version>1.0.1-RELEASE</version>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.9</java.version>
</properties>

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.apache.sshd/apache-sshd -->
    <dependency>
        <groupId>org.apache.sshd</groupId>
        <artifactId>apache-sshd</artifactId>
        <version>1.7.0</version>
        <type>pom</type>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-jdk14</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.25</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/net.java.dev.jna/jna -->
    <dependency>
        <groupId>net.java.dev.jna</groupId>
        <artifactId>jna</artifactId>
        <version>4.5.1</version>
    </dependency>
    <dependency>
        <groupId>net.java.dev.jna</groupId>
        <artifactId>jna-platform</artifactId>
        <version>4.5.1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>5.0.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.0.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
        <version>2.0.0.RELEASE</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
                <descriptors>
                    <descriptor>assembly.xml</descriptor>
                </descriptors>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <source>1.9</source>
                <target>1.9</target>
            </configuration>
        </plugin>
    </plugins>
</build>

Edit2: 我尝试将以下内容添加到maven-compiler-plugin配置中:

<compilerArgs>
    <arg>--add-modules</arg><arg>spring.web</arg>
</compilerArgs>

然后尝试用“mvn compile -X”编译它以获得完整的调试日志,所以我得到了执行编译的命令行:

[INFO] Changes detected - recompiling the module!
[DEBUG] Classpath:
[DEBUG] Source roots:
[DEBUG]  D:\Work\gitrepos\mymoduleproject\src\main\java
[DEBUG]  D:\Work\gitrepos\mymoduleproject\target\generated-sources\annotations
[DEBUG] Command line options:
[DEBUG] -d D:\Work\gitrepos\mymoduleproject\target\classes -sourcepath D:\Work\gitrepos\mymoduleproject\src\main\java;D:\Work\gitrepos\mymoduleproject\target\generated-sources\annotations; -s D:\Work\
gitrepos\mymoduleproject\target\generated-sources\annotations -g -nowarn -target 1.9 -source 1.9 -encoding UTF-8 --add-modules spring.web

但又一次......我明白了

module-info.java:[2,20] module not found: spring.web

所有其他模块都一样......

Edit3:

我创建了一个新项目,新项目编译得很好(但我仍然需要编译原始项目!) . 我做了同样的“mvn compile -X”命令,这是我得到的:

[INFO] Changes detected - recompiling the module!
[DEBUG] Classpath:
[DEBUG]  C:\Users\myuser\.m2\repository\org\springframework\spring-web\5.0.4.RELEASE\spring-web-5.0.4.RELEASE.jar
[DEBUG]  C:\Users\myuser\.m2\repository\org\springframework\spring-beans\5.0.4.RELEASE\spring-beans-5.0.4.RELEASE.jar
[DEBUG]  C:\Users\myuser\.m2\repository\org\springframework\spring-core\5.0.4.RELEASE\spring-core-5.0.4.RELEASE.jar
[DEBUG]  C:\Users\myuser\.m2\repository\org\springframework\spring-jcl\5.0.4.RELEASE\spring-jcl-5.0.4.RELEASE.jar
[DEBUG]  C:\Users\myuser\.m2\repository\org\springframework\boot\spring-boot-starter\2.0.0.RELEASE\spring-boot-starter-2.0.0.RELEASE.jar
[DEBUG]  C:\Users\myuser\.m2\repository\org\springframework\spring-aop\5.0.4.RELEASE\spring-aop-5.0.4.RELEASE.jar
[DEBUG]  C:\Users\myuser\.m2\repository\org\springframework\spring-expression\5.0.4.RELEASE\spring-expression-5.0.4.RELEASE.jar
[DEBUG]  C:\Users\myuser\.m2\repository\org\springframework\boot\spring-boot-starter-logging\2.0.0.RELEASE\spring-boot-starter-logging-2.0.0.RELEASE.jar
[DEBUG]  C:\Users\myuser\.m2\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar
[DEBUG]  C:\Users\myuser\.m2\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar
[DEBUG]  C:\Users\myuser\.m2\repository\org\apache\logging\log4j\log4j-to-slf4j\2.10.0\log4j-to-slf4j-2.10.0.jar
[DEBUG]  C:\Users\myuser\.m2\repository\org\apache\logging\log4j\log4j-api\2.10.0\log4j-api-2.10.0.jar
[DEBUG]  C:\Users\myuser\.m2\repository\org\slf4j\jul-to-slf4j\1.7.25\jul-to-slf4j-1.7.25.jar
[DEBUG]  C:\Users\myuser\.m2\repository\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar
[DEBUG]  C:\Users\myuser\.m2\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar
[DEBUG] Modulepath:
[DEBUG]  C:\Users\myuser\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\2.0.0.RELEASE\spring-boot-autoconfigure-2.0.0.RELEASE.jar
[DEBUG]  C:\Users\myuser\.m2\repository\org\springframework\boot\spring-boot\2.0.0.RELEASE\spring-boot-2.0.0.RELEASE.jar
[DEBUG]  D:\Work\gitrepos\demo\javamodules\target\classes
[DEBUG]  C:\Users\myuser\.m2\repository\org\springframework\spring-context\5.0.4.RELEASE\spring-context-5.0.4.RELEASE.jar
[DEBUG] Source roots:
[DEBUG]  D:\Work\gitrepos\demo\javamodules\src\main\java
[DEBUG]  D:\Work\gitrepos\demo\javamodules\target\generated-sources\annotations
[DEBUG] Command line options:
[DEBUG] -d D:\Work\gitrepos\demo\javamodules\target\classes -classpath C:\Users\myuser\.m2\repository\org\springframework\spring-web\5.0.4.RELEASE\spring-web-5.0.4.RELEASE.jar;C:\Users\myuser\.m2\repository\
org\springframework\spring-beans\5.0.4.RELEASE\spring-beans-5.0.4.RELEASE.jar;C:\Users\myuser\.m2\repository\org\springframework\spring-core\5.0.4.RELEASE\spring-core-5.0.4.RELEASE.jar;C:\Users\myuser\.m2\re
pository\org\springframework\spring-jcl\5.0.4.RELEASE\spring-jcl-5.0.4.RELEASE.jar;C:\Users\myuser\.m2\repository\org\springframework\boot\spring-boot-starter\2.0.0.RELEASE\spring-boot-starter-2.0.0.RELEAS
E.jar;C:\Users\myuser\.m2\repository\org\springframework\spring-aop\5.0.4.RELEASE\spring-aop-5.0.4.RELEASE.jar;C:\Users\myuser\.m2\repository\org\springframework\spring-expression\5.0.4.RELEASE\spring-expres
sion-5.0.4.RELEASE.jar;C:\Users\myuser\.m2\repository\org\springframework\boot\spring-boot-starter-logging\2.0.0.RELEASE\spring-boot-starter-logging-2.0.0.RELEASE.jar;C:\Users\myuser\.m2\repository\ch\qos\lo
gback\logback-classic\1.2.3\logback-classic-1.2.3.jar;C:\Users\myuser\.m2\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;C:\Users\myuser\.m2\repository\org\apache\logging\log4j\log4j-to-
slf4j\2.10.0\log4j-to-slf4j-2.10.0.jar;C:\Users\myuser\.m2\repository\org\apache\logging\log4j\log4j-api\2.10.0\log4j-api-2.10.0.jar;C:\Users\myuser\.m2\repository\org\slf4j\jul-to-slf4j\1.7.25\jul-to-slf4j-
1.7.25.jar;C:\Users\myuser\.m2\repository\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;C:\Users\myuser\.m2\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar; --module-p
ath C:\Users\myuser\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\2.0.0.RELEASE\spring-boot-autoconfigure-2.0.0.RELEASE.jar;C:\Users\myuser\.m2\repository\org\springframework\boot\spring-
boot\2.0.0.RELEASE\spring-boot-2.0.0.RELEASE.jar;D:\Work\gitrepos\demo\javamodules\target\classes;C:\Users\myuser\.m2\repository\org\springframework\spring-context\5.0.4.RELEASE\spring-context-5.0.4.RELEAS
E.jar; -sourcepath D:\Work\gitrepos\demo\javamodules\src\main\java;D:\Work\gitrepos\demo\javamodules\target\generated-sources\annotations; -s D:\Work\gitrepos\demo\javamodules\target\generated-sources\an
notations -g -parameters -nowarn -target 1.9 -source 1.9 -encoding UTF-8

为什么我在新项目中获得“模块路径”以及Maven命令行中的许多其他选项而我在原始项目中没有得到它们?

1 回答

  • 3

    好吧,问题出现在pom.xml中!我在编译过程中遇到“模块未找到”错误的原因隐藏在警告背后,我认为这并不重要,因此我没有在日志中粘贴 . 警告是“找不到zip END Headers ” . 得到此警告的原因是因为我的依赖项中存在“格式错误的jar”,因此编译器在解析module-info.java文件时失败并产生上述错误 . 但哪一个是“畸形”的 jar ?它出来是这个(我一个接一个地使用排除的方法......):

    <dependency>
            <groupId>org.apache.sshd</groupId>
            <artifactId>apache-sshd</artifactId>
            <version>1.7.0</version>
            <type>pom</type>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-jdk14</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    

    我在搜索apache sshd时从https://mvnrepository.com获得了依赖性,我猜测,因为Apache Mina是一组几个项目,所以你包括所有项目......无论如何,我只需要apache-sshd-core来自所有项目,所以我将依赖项更改为以下(Apache Mina Dev社区为我提供了建议,所以非常感谢他们!!!):

    <dependency>
        <groupId>org.apache.sshd</groupId>
        <artifactId>sshd-core</artifactId>
        <version>1.7.0</version>
    </dependency>
    

    现在所有编译都很好!非常感谢大家的评论和帮助!

相关问题