首页 > 解决方案 > com.faizan.org 包可从多个模块访问:在 Eclipse 2019-12 构建期间使用 JDK 9+ 的 ProjectA、ProjectB

问题描述

我有 2 个项目,比如 ProjectA 和 ProjectB,都包含包 com.faizan.org。ProjectA 被添加到 ProjectB 的模块路径中。

<classpathentry combineaccessrules="false" kind="src" path="/ProjectA">
    <attributes>
        <attribute name="module" value="true"/>
    </attributes>
</classpathentry>

现在我正在 ProjectB 中编写一个新类,该类需要从 ProjectA 的 com.faizan.org 导入一个类,但出现错误 The package com.faizan.org is access to more module: ProjectA, ProjectB in eclipse 2019 -12 使用 openJdk 12 并且编译器合规性也设置为 12。

如何在没有类路径冲突的情况下将包含相同包名的外部项目添加到另一个项目?此外,在某些情况下无法访问超类的方法。

标签: classpathjava-9superclassmodule-info

解决方案


Simple answer: you cannot.

When you setup your Eclipse projects as Java modules, then the rules of the module system JPMS forbid that any module has access to the same package from two modules (each package must be "uniquely visible").

Next, you should revisit why you need to have the same package in both projects? If it is for whitebox testing, then please consider moving the tests to the same project, but in a separate source folder marked as containing tests. Then Eclipse will do all the necessary wiring behind the scenes, so that the tests are part of the module and not part of the module at the same time.

If it's not for the sake of whitebox testing, and you do want to adopt JPMS, then you are left with 2.5 options:

  1. Move all code that shares a package into the same project / module.
  2. Change the package structure to avoid the split package.
  3. (Use a tricky set of JPMS options including --patch-module and likely more to let JPMS view the separate projects as one module -- while possible I would consider this as "successful migration")

推荐阅读