【问题标题】:Java 11: why "The import xxx cannot be resolved" even with module-info informing it is requiredJava 11:为什么“无法解析导入 xxx”,即使需要模块信息通知
【发布时间】:2019-03-20 18:33:36
【问题描述】:

自 Java 9 以来,我已经阅读了有关模块化的信息。我知道我必须创建一个模块信息,并了解哪些包是公开的和需要的。我可以看到我的类路径中确实有 Java 11。但是我收到了主题中提到的错误。更准确地说,在构建时,我得到了这个错误日志

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

为了让普通项目中的类对其他项目可见,我缺少一些额外的设置?

PS1:我关注Import XXX cannot be resolved for Java SE standard classes 并设置为替代 JRE。 PS2.:我认为这与 Eclipse 无关。顺便说一句,我正在使用这个版本:

面向企业 Java 开发人员的 Eclipse IDE。 版本:2018-12 (4.10.0) 版本号:20181214-0600 操作系统:Windows 10、v.10.0、x86_64 / win32 Java版本:11.0.2

在我的zuul项目中:

import com.test.common.security.JwtConfig;

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

...

模块信息.java

module zuul {
...
    requires common;

}

在我的共同项目中

@Getter
@ToString
public class JwtConfig {

...

模块信息.java

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

【问题讨论】:

    标签: eclipse java-9 java-11


    【解决方案1】:

    我发现了问题。对于其他对 Java9+ 更有经验的人来说,这可能看起来很傻,但对我来说,我花了一段时间才找到根本原因。希望对以后的读者有用。

    问题:common 项目在 classpath 而不是 module-path

    解决方案:只需按照我移交时 Eclipse 的建议移至 module-path 需要 common;在 module-info.java 移交时 import com.test.common.security.JwtConfig;在 SecurityTokenConfig.java 上,提出的解决方案与我移交 module-info.java 时的解决方案不同(也许对 Eclipse 团队的一个适度建议是从现在开始对其进行修订,但这超出了这个问题的目的)。

    感谢https://www.eclipse.org/community/eclipse_newsletter/2018/june/java9andbeyond.php

    ***已编辑

    虽然我仍然认为上述解决方案是我问题的答案,但我必须承认,在解决了这个问题后,我偶然发现了一个新问题,并且我意识到,至少就我而言,即使使用 Java 11,我也必须避免模块化(拼图)。未来的读者可能也会对此线程感兴趣

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-30
      • 2018-04-28
      • 1970-01-01
      • 2011-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多