找不到Java 9自动模块 [英] Java 9 automatic modules not found

查看:125
本文介绍了找不到Java 9自动模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to define a Java 9 module. I have defined something like:

module my.module.name {

}

然后我的许多文件开始给我错误,它们找不到某些软件包.然后,我使用了IntelliJ的自动帮助功能,并将其添加到我的module-info.java中的几个"requires"语句.这样就变成了:

Then many of my files started to give me errors, that they cannot find some packages. Then I used the auto-help feature of IntelliJ and it added to my module-info.java several "requires" statements. So it became something like:

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

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

Now IntelliJ shows all my code without errors. But when I click "compile" in the "Maven Projects" window (I'm using Maven 3.5.3 and Oracle JDK 10 for compilation), it tells me the following:

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".

据我了解,未定义为模块的第三方库会生成自动"模块.所以我找到了我需要的一个罐子:

As far as I understood, 3rd party libraries that are not defined as modules generate "automatic" modules. So I found one of the jars that I require:

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

,并希望通过以下命令查看自动"模块的名称:

and wanted to see what is the name of the "automatic" module with the following command:

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
...

因此,根据我的调查,我需要"的模块名称是正确的!我想念什么?为什么会得到

So according to my investigation, the name of the module I "require" is correct! What am I missing? Why do I get

module not found: spring.web

与其他模块相同吗?我是否缺少某些路径或其他东西?

and same for the other modules? Am I missing some paths or something?

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

Edit: as requested, here is my 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配置中:

I tried to add the following to the maven-compiler-plugin configuration:

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

,然后尝试使用"mvn compile -X"进行编译以获取完整的调试日志,因此我执行了用于编译的命令行:

and then tried to compile it with "mvn compile -X" to get full debug log, so I got the command line executed for compilation:

[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

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

and same for all the other modules ...

Edit3:

我创建了一个新项目,并且新项目可以正常编译(但是我仍然需要对原始项目进行编译!).我执行了相同的"mvn compile -X"命令​​,这就是我得到的:

I created a new project and the new one compiles fine (but I still need the original project getting compiled!). I did the same "mvn compile -X" command and here is what I get:

[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命令行中却获得了许多其他选项,而在我的原始项目中却没有得到它们?

Why do I get "Module path" in the new project and so many other options in the Maven command line and I don't get them in my original project?

推荐答案

好的,问题出在pom.xml中!我在编译过程中遇到找不到模块"错误的原因隐藏在警告中,我认为这并不重要,因此我没有将其粘贴在日志中.警告是找不到zip END标头".收到此警告的原因是因为我的依赖项中包含格式错误的jar",因此编译器在解析module-info.java文件时失败并产生了上述错误.但是畸形的"罐子是哪一个?出来的就是这个(我用一种一一排除的方法...):

Alright, the problem came out to be in the pom.xml! The reason I was getting "module not found" error during compilation was hidden behind a WARNING which I was thinking is not important, so I did not paste here in the logs. The warning was "zip END header not found". The reason for getting this warning was because I was having a "malformed jar" in my dependencies and thus the compiler was failing during parsing the module-info.java file and was producing the above errors. But which one was the "malformed" jar? It came out that it is this one (I used the method of exclusion one by one ... ):

    <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 中获得了这种依赖关系,我不是完全正确确定"type pom"是什么意思,但是我猜是因为Apache Mina是一个由多个项目组成的集合,所以您将所有项目都包括在内...无论如何,我只需要从所有项目,所以我将依赖关系更改为以下内容(Apache Mina Dev社区向我建议了这一点,因此非常感谢他们!!):

I got that dependency from https://mvnrepository.com when searching for apache sshd, I'm not exactly sure what does "type pom" mean, but I'm guessing, that because Apache Mina is a set of several projects, in this way you include all the projects ... anyway, I only needed apache-sshd-core from all the projects, so I changed the dependency to the following (the Apache Mina Dev community advised me about that, so thanks a lot to them!!!):

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

,现在所有编译都可以了!非常感谢大家的评论和帮助!

and now all compiles just fine! Thanks a lot to everyone for the comments and the help!

这篇关于找不到Java 9自动模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆