Automatic modules not found in Eclipse 2018-12 when project is opened

淺唱寂寞╮ 提交于 2019-12-10 21:46:18

问题


I want to switch from Oracle JDK 8 to Open JDK 11 using Eclipse 2018-12. I have the following situation:

In my Eclipse workspace I have a main maven project called example with some maven dependencies my_dependency_1, my_dependency_2 that are also maven projects in the same workspace. Currently, only example is modular (and thus contains a module-info.java). The other projects are non-modular and are included in the pom.xml as well as required (automatic) modules in the module-info.java of example. I recognized the following:

When I close all Eclipse projects in my workspace except example, then all (non-modular) dependency .jars get correctly included from the .m2 repository in the module-path as automatic modules and my example project runs fine. I can verify this by looking at Run Configurations > Arguments > Show Command Line.

In contrast, when I open the projects with my dependencies in my workspace as well, the dependencies get not included into the module-path and thus the example project doesn't run (resulting in Error occurred during initialization of boot layer, java.lang.module.FindException: Module my_dependency_1, ... not found, required by example).

My question is: Do I need to convert all my_dependencies_1, ... from non-modular to modular dependencies if I want to open and work with them along with my modular example project in my workspace, or is there any option that lets me keep working even with the non-modular dependency projects? I'm looking for a simple and clean solution and not for a "hack" with manually appending funky stuff to the startup arguments. If this is not possible I'd prefer to take some time to convert each dependency into a modular one with the drawback of being not usable anymore in other projects still written for Java 8.

Thanks for clarification :)

Here is a basic example for demonstration:

pom.xml (of project example)

<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>com.example</groupId>
  <artifactId>example</artifactId>
  <version>1.0-SNAPSHOT</version>

  <dependencies>
    <dependency>
      <groupId>com.example</groupId>
      <artifactId>mydependency</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>
</project>

Example.java (of project example)

package example;
import mydependency.MyDependency;

public class Example {
    public static void main(String[] args) { 
        MyDependency.run();
    }
}

module-info.java (of project example)

open module example {
    requires mydependency;
}

pom.xml (of project mydependency)

<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>com.example</groupId>
  <artifactId>mydependency</artifactId>
  <version>1.0-SNAPSHOT</version>
</project>

MyDependency.java (of project mydependency)

package mydependency;
public class MyDependency {
    public static void run() {
        System.out.println("run");
    }
}

Command line when running if project is closed (works properly):

C:\me\jdk-11.0.1\bin\javaw.exe
-Dfile.encoding=UTF-8
-p "C:\me\workspace\example\target\classes;C:\me\.m2\repository\com\example\mydependency\1.0-SNAPSHOT\mydependency-1.0-SNAPSHOT.jar"
-m example/example.Example

Command line when running if project is opened (target/classes of mydependency is missing):

C:\me\jdk-11.0.1\bin\javaw.exe
-Dfile.encoding=UTF-8
-p "C:\me\workspace\example\target\classes"
-m example/example.Example

回答1:


A source project cannot possibly be an automatic module, because the name of an automatic module is derived from the jar filename.

What you need is:

  • mvn install in each dependency
  • Maven > Disable Workspace Resolution on each project that depends on an automatic module (context menu in the Package Explorer)

Edits:

  • Alternatively, instead of disabling workspace resolution, all projects containing automatic modules can be closed.
  • People affected by this may champion Eclipse Bug 543925, where a cooperation between m2e and JDT could go beyond what is specified by JPMS: m2e could tell JDT what would be the automatic module name, if a given project would be accessed from it's jar file. Behind the scenes running that program would then require to ignore the workspace resolution, so that the JVM finds the needed jar file.


来源:https://stackoverflow.com/questions/54195765/automatic-modules-not-found-in-eclipse-2018-12-when-project-is-opened

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