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

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

问题描述

我正在开发一个 Maven 项目(分支平台-bom_brussels-sr7)在 Eclipse 中.当我最近尝试将项目的 Java 构建路径切换到 JDK 10 时,Eclipse 构建无法再找到诸如 javax.xml.xpath.XPathorg.w3c.dom.Document 之类的类org.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 时均失败.

推荐答案

我假设从 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.

这种情况违反了7.html#jls-7.4.3" rel="noreferrer">JLS §7.4.3(最后一段).特别是每个限定类型名称 Q.Id (JSL §6.5.5.2) 要求其前缀 Q 是唯一可见的包(为了简单起见,我忽略了嵌套类型的情况).因此:程序是非法的,必须被编译器拒绝.

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控制你的项目读取哪个模块,或者requiresjava.xml; or 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 中的等效项是 "Modularity Details" 对话框,另见JDT 4.8 New&Noteworthy(查找内容标签).由于 java.xml 是通过许多其他默认可观察模块隐式需要的,因此从右侧推送除 java.base 之外的所有内容可能是一个好主意(明确地包含的模块")向左(可用模块")(并有选择地重新添加您的项目需要的那些模块).

(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 年 4 月 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天全站免登陆