Java 9 automatic modules not found

自古美人都是妖i 提交于 2019-12-31 22:24:10

问题


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

module my.module.name {

}

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

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?

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: I tried to add the following to the maven-compiler-plugin configuration:

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

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

but again ... I get

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

and same for all the other modules ...

Edit3:

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

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?


回答1:


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>

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!



来源:https://stackoverflow.com/questions/50353001/java-9-automatic-modules-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!