Eclipse can't find XML related classes after switching build path to JDK 10
I assume that the project being migrated from Java 1.8 still has no module-info.java
. This implies you are compiling code in the "unnamed module".
Code in the unnamed module "reads" all observable named and unnamed modules, in particular it reads module "java.xml" from the JRE System Library. This module exports package like java.xml.xpath
.
Additionally, you have xml-apis.java
on the classpath, which contributes another set of packages of the same names (java.xml.xpath
and friends). These are said to be associated to the unnamed module, like your own code.
This situation violates the requirement of "unique visibility" as defined in JLS §7.4.3 (last paragraph). In particular every qualified type name Q.Id (JSL §6.5.5.2) requires that its prefix Q is a uniquely visible package (I'm disregarding the case of nested types for simplicity). Ergo: the program is illegal and must be rejected by compilers.
This leaves us with one question and two solutions:
(1) Question: Why is javac accepting the program?
(2) Solution: If you add module-info.java
to your project, you can control via requires which module your project reads, either requires java.xml;
or requires xml.apis;
(where "xml.apis" is the automatic module name of "xml-apis-1.4.01.jar).
(3) Solution: Short of turning your project into a module, you can still avoid the conflict by excluding java.xml
from the set of observable modules. On the command line this would be done using --limit-modules
. The equivalent in Eclipse is the "Modularity Details" dialog, see also the JDT 4.8 New&Noteworthy (look for Contents tab). Since java.xml
is implicitly required via a lot of other default-observable modules, it may be a good idea to push everything except for java.base
from right ("Explicitly included modules") to left ("Available modules") (and selectively re-add those modules that your project needs).
PS: Eclipse still doesn't provide an ideal error message, instead of "cannot be resolved" it should actually say: "The package javax.xml.xpath is accessible from more than one module: javax.xml, <unnamed>.
PPS: Also weird: how come that changing the order between JRE and a jar on the classpath (such ordering is not a concept supported by javac nor JEP 261) changes the behavior of the compiler.
EDITs:
- Alex Buckley confirmed that the given situation is illegal, despite what javac says. Bug against javac has been raised as JDK-8215739. This bug has been acknowledged months before the release of Java 12. As of 2019-06 it has been decided that also Java 13 will ship without a fix. Similarly for Java 14. The bug was temporarily scheduled for Java 15, but this plan has been dropped on 2020-04-20.
- Eclipse error message has been improved to mention the real problem.
- In Eclipse 2019-06 the UI used for Solution (3) has been revamped. Up-to-date documentation can be found in the online help.
Eclipse module problems jdk 10
I'm not sure I count this as a true solution, but I was able to find a workaround to my problem. I downgraded eclipse from the installed version listed in the question (4.11.0) to the base photon install (4.8.0), and I can now compile and work again.
Eclipse is confused by imports (accessible from more than one module)
This is caused by
- a JAR on the Classpath that contains the package
java.awt
that also exists in the system library but the - JRE System Library is on the Modulepath
In the Java Platform Module System (JPMS) it is not allowed to use the same package in more than one module. If the Modulepath and the Classpath is used, everything on the Classpath is handled as the <unnamed>
module (in your case the package java.awt
exists in the system module java.desktop
and also via the JAR on the Classpath in the module <unnamed>
).
Since the JRE System Library cannot be moved from the Modulepath to the Classpath (see this answer by Stephan Herrmann for details), you only have the following options:
- Set the compiler compliance to 1.8 (as you already mentioned)
- Rebuilt the JAR to avoid Java system library package names inside the JAR (if reflection is used, additional code changes may be necessary):
- If you have the source code, change the package names (e.g. change the package and subpackae
java
tojava_util
andjavax
tojavax_util
) and recreate the JAR - If you have only the
.class
files you have to decompile the.class
files first
- If you have the source code, change the package names (e.g. change the package and subpackae
Java build issues - Java 8 to Java 11 migration
Starting from version 9 Java supports JPMS (Java Platform Module System).
In the Java Platform Module System it is not allowed to use the same package in more than one module. So, the solution is to find modules (jars) that exports this package org.w3c.dom
and to remove one of the modules or package in it.
Related Topics
How to Use Optional Parameters in Java
Why Doesn't Java Offer Operator Overloading
Can Enums Be Subclassed to Add New Elements
How to Convert Hashmap to JSON Object in Java
Java Maximum Memory on Windows Xp
Java Event-Dispatching Thread Explanation
Which Overload Will Get Selected for Null in Java
How to Configure Port for a Spring Boot Application
Why Should Java 8's Optional Not Be Used in Arguments
Java: Export to an .Jar File in Eclipse
Java Multi-Threading & Safe Publication
Eclipse Exported Runnable Jar Not Showing Images
Multiple Returns: Which One Sets the Final Return Value