将构建路径切换到JDK 10后,Eclipse无法找到与XML相关的类 [英] Eclipse can't find XML related classes after switching build path to JDK 10

查看:94
本文介绍了将构建路径切换到JDK 10后,Eclipse无法找到与XML相关的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 Maven项目(分支平台-bom_brussels-sr7)在Eclipse中.当我最近尝试将项目的Java构建路径切换到JDK 10时,Eclipse构建不再能够找到诸如javax.xml.xpath.XPathorg.w3c.dom.Documentorg.xml.sax.SAXException之类的类.似乎只有与XML相关的类会受到影响,主要是受到Maven依赖xml-apis-1.4.01的影响.

I'm developing on a Maven project (branch platform-bom_brussels-sr7) in Eclipse. When I recently tried switching the Java Build Path for the project to JDK 10, Eclipse build can no longer find classes such as javax.xml.xpath.XPath, org.w3c.dom.Document, or org.xml.sax.SAXException. It seems only XML related classes are impacted, mostly from the Maven dependency xml-apis-1.4.01.

尝试从Eclipse进行Maven构建时不会出错.假定缺少的类之一上的Ctrl-LeftClick将找到该类并在Eclipse编辑器中将其打开.看来只有Eclipse构建受到影响.

Trying a Maven build from Eclipse works without errors. Ctrl-LeftClick on one of the supposedly missing classes finds the class and opens it in the Eclipse editor. It seems only the Eclipse build is impacted.

我尝试了几件事,但没有帮助.我试过了:

I tried several things, but none helped. I tried:

  • 清洁项目
  • 不同的Eclipse版本:氧气和光子.
  • 使用JDK 8和JDK 10运行Eclipse本身.
  • 更改项目的编译器合规性级别.它在JDK 8的构建路径下以遵从性级别8和10进行构建,而在JDK 10的构建路径中均以失败告终.
  • Project Clean
  • Different Eclipse Versions: Oxygen and Photon.
  • Running Eclipse itself with JDK 8 and JDK 10.
  • Changing Compiler Compliance level for the project. It builds with compliance level 8 and 10 under JDK 8 build path and fails for both with JDK 10 in build path.

推荐答案

我假定从Java 1.8迁移的项目仍然没有module-info.java.这意味着您正在未命名的模块"中编译代码.

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".

未命名模块中的代码读取"所有可观察到的命名和未命名模块,特别是它从JRE系统库中读取模块"java.xml".此模块导出包,如java.xml.xpath.

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.

此外,您在类路径上有xml-apis.java,它贡献了另一组相同名称的包(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)问题:为什么Javac接受该程序?

(1) Question: Why is javac accepting the program?

(2)解决方案:如果将module-info.java添加到项目中,则可以通过 requires 控制项目读取的是哪个模块,requires java.xml;requires xml.apis;(其中"xml.apis "是"xml-apis-1.4.01.jar)的自动模块名称.

(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)解决方案:除了将您的项目变成一个模块之外,您仍然可以通过将java.xml从可观察模块的集合中排除来避免冲突.在命令行上,这可以使用--limit-modules完成. Eclipse中的等效项是,另请参见

(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仍然没有提供理想的错误消息,而不是无法解决",它实际上应该说:包javax.xml.xpath可以从多个模块访问:javax.xml,<未命名>.

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:也很奇怪:改变JRE和类路径上的jar之间的顺序(这种顺序不是javac或JEP 261不支持的概念)怎么会改变编译器的行为.

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.

  • Alex Buckley 确认尽管javac说了什么,但给定的情况是非法的.针对Javac的错误已提出为 JDK-8215739 .在Java 12发行前几个月就已经确认了该错误.从2019年6月起,已经确定Java 13也将在未修复的情况下发布.对于Java 14同样如此.该错误是Java 15的临时计划,但该计划已在2020-04-20删除.
  • Eclipse错误消息已得到改进提及了真正的问题.
  • 在Eclipse 2019-06中,用于解决方案(3)的UI已已更新.可以在
  • 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.

这篇关于将构建路径切换到JDK 10后,Eclipse无法找到与XML相关的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆