问题
While migrating our project from Java 8 to Java 11 modules, I have a question concerning the Java Module System (JPMS), especially to the required transitive directive. Lets assume we have four simple projects A, B, C and D with the following module declarations in module-info.java:
Project A
open module a {
exports a;
}
Project B
open module b {
exports b;
requires transitive a;
}
Project C
open module c {
exports c;
requires transitive b;
}
Project D
open module d {
requires transitive c;
}
Using Maven, we declared the dependencies D depends on C, C depends on B and B depends on A in the corresponding pom.xml files. Besides, each project contains a class file with the following content (the files for projects A, B and C are equivalent):
package d;
public class D {
public static void main(String[] args) {
System.out.println("D");
}
}
When running the classes using the default runtime configuration of Eclipse 2018-12, only class A, B and C runs without errors. But when I run class D I get: Error occurred during initialization of boot layer java.lang.module.FindException: Module a not found, required by b
In my understanding module a should be known to module b, module c and also module d due to applied requires transitive directives. But as you can see, at runtime this is not the case since module a is not set in the module-path (-p) but on the classpath of the generated command line:
C:\me\jdk-11.0.1\bin\javaw.exe
-Dfile.encoding=UTF-8
-p "C:\me\workspace\d\target\classes;C:\me\workspace\c\target\classes;C:\me\workspace\b\target\classes"
-classpath "C:\me\workspace\a\target\classes"
-m d/d.D
So my question is: Why project A (C:\me\workspace\a\target\classes) has been added to the classpath and not to the module-path? Is this intended behaviour, and what do I have to change to get project D running in Eclipse?
If I move C:\me\workspace\a\target\classes to the -p argument list, everything works as I would expect but that's surely not the preferred way to go.
Thank you for any hints.
来源:https://stackoverflow.com/questions/54215858/java-module-not-found-at-runtime-even-with-requires-transitive