Search code examples
eclipsejava-9java-11

Java 11: why "The import xxx cannot be resolved" even with module-info informing it is required


I have read about modularity since Java 9. I am aware I have to create a module-info and infor which package is exposed and required. I can see I do have Java 11 on my classpath. But I am getting the error mentioned on topic. More precisely, while building, I get this error log

INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ zuul ---
[WARNING] Can't extract module name from xpp3_min-1.1.4c.jar: Provider class org.xmlpull.mxp1.MXParser,org.xmlpull.mxp1_serializer.MXSerializer not in module
[WARNING] ********************************************************************************************************************
[WARNING] * Required filename-based automodules detected. Please don't publish this project to a public artifact repository! *
[WARNING] ********************************************************************************************************************
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to C:\_d\WSs\soteste\zuul\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /C:/_d/WSs/soteste/zuul/src/main/java/module-info.java:[24,18] module not found: common
[INFO] 1 error

I am missing some extra setup in order to make the class from common project visible to other project?

PS1: I followed Import XXX cannot be resolved for Java SE standard classes and setup to alternative JRE. PS2.: I don't think this has something related to Eclipse. BTW, I am using this version:

Eclipse IDE for Enterprise Java Developers. Version: 2018-12 (4.10.0) Build id: 20181214-0600 OS: Windows 10, v.10.0, x86_64 / win32 Java version: 11.0.2

In my zuul project:

import com.test.common.security.JwtConfig;

@EnableWebSecurity  
public class SecurityTokenConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private JwtConfig jwtConfig;

...

module-info.java

module zuul {
...
    requires common;

}

In my common project

@Getter
@ToString
public class JwtConfig {

...

module-info.java

module common {
    exports com.test.common.security;
    exports com.test.common;
...
}

Solution

  • I found the issue. Probably it will seem very silly for others more experienced with Java9+ but for me took a while to find the root cause. Hopefully it can be usefull for future readers.

    The problem: common project was in classpath instead of module-path

    enter image description here

    The solution: just move to module-path as sugested by Eclipse when I handed over requires common; in module-info.java When I handovered import com.test.common.security.JwtConfig; on SecurityTokenConfig.java the solution proposed is completed diferent from the one when I handed over module-info.java (maybe a modest suggestion to Eclipse team would be revview it from now on but it is beyond the purpose of this question).

    enter image description here

    Credits to https://www.eclipse.org/community/eclipse_newsletter/2018/june/java9andbeyond.php

    *** edited

    Although I still consider the above solution as the answer to my question I must admit that after fixed this issue I stumbled on a new issue and I realize that, at least for my case, even using Java 11 I must avoid modularity (Jigsaw). Future readers may get interested on this thread also

    Java 11 without modularity: package does not exist while it is added as maven dependency