Eclipse jdk 11问题:无法解析java.lang.String类型。它是从必需的.class文件间接引用的

Eclipse jdk 11问题:无法解析java.lang.String类型。它是从必需的.class文件间接引用的,java,eclipse,java-11,openjdk-11,Java,Eclipse,Java 11,Openjdk 11,我有一个玩具程序,当我试图使用编年史导入并编译到语言级别11时,它只有在Eclipse中出现编译错误。该程序在maven和IntelliJ(使用相同的maven和JDK)中编译和运行 我的版本是: maven 3.6.1 jdk openjdk版本“11”2018-09-25 eclipse 2020-03-R 编年史bom 2.19.199(自2.17以来支持java11) 这是我的pom.xml <project xmlns="http://maven.apache.org/POM

我有一个玩具程序,当我试图使用编年史导入并编译到语言级别11时,它只有在Eclipse中出现编译错误。该程序在maven和IntelliJ(使用相同的maven和JDK)中编译和运行

我的版本是:

  • maven 3.6.1
  • jdk openjdk版本“11”2018-09-25
  • eclipse 2020-03-R
  • 编年史bom 2.19.199(自2.17以来支持java11)
这是我的pom.xml

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>testjava11</groupId>
    <artifactId>chronicle-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>net.openhft</groupId>
                <artifactId>chronicle-bom</artifactId>
                <version>2.19.199</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>net.openhft</groupId>
            <artifactId>chronicle-map</artifactId>
        </dependency>
    </dependencies>
</project>
使用maven直接运行exec:java时的输出是

[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------< testjava11:chronicle-test >----------------------
[INFO] Building chronicle-test 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ chronicle-test ---
[INFO] Deleting C:\Users\eclipse-workspace\chronicle-test\target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ chronicle-test ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform     dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ chronicle-test ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform     dependent!
[INFO] Compiling 1 source file to C:\Users\eclipse-workspace\chronicle-test\target\classes
[INFO] 
[INFO] --- exec-maven-plugin:3.0.0:java (default-cli) @ chronicle-test ---
BytesMarshallable: App$1@309d6b5b
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.400 s
[INFO] Finished at: 2020-06-11T15:04:53+02:00
[INFO] ------------------------------------------------------------------------
但是,在Eclipse中,类应用程序将不会编译。错误出现在导入行上,并显示:

The type java.lang.String cannot be resolved. It is indirectly referenced from required .class files
“问题”面板中还有一条消息:

The project was not built since its build path is incomplete.
Cannot find the class file for java.lang.String.
Fix the build path then try building this project
但我看不出我的构建路径中存在什么问题:

此外,我可以看到java.lang出现在包资源管理器中:

(请注意,如果我将语言级别更改为8,但仍然使用JDK11,它将在Eclipse中工作。)


我已经检查了明显的问题(构建路径、maven/jdk路径),我觉得一切都是正确的。为什么在Eclipse中会出现此错误?如何修复它?

它与pom文件不匹配,因此也在pom文件中更新

<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
11
11

我已经使用了这两个,那么它对我来说工作正常。

完整的错误消息是:

  • 类型java.lang.String是从必需的.class文件间接引用的,但无法解析,因为声明包java.lang已导出 from module java.base与可从模块访问的包冲突
这是由一个可传递依赖项net.openhft:affinity:3.2.3引起的,它嵌入了java.lang包中的2个类,这是非法的。Eclipse中的ECJ编译器会抱怨它。然而,它在javac中工作的事实本身就是一个bug:

这其中有一个亲和力问题:


如果您没有使用线程关联特性,只需从依赖项中排除关联,Eclipse编译器将停止抱怨。

在我的例子中,我有以下错误,它与“配置生成路径”没有关系--


在我创建了一个包并将有问题的类定位到类中后,错误得到了解决。

也许您没有在Window>Preferences:Java>Installed JRE中配置JDK。否则,看看Eclipse告诉您的项目(构建)问题。根本原因是项目或Eclipse的配置错误,而不是字符串无法解决。我添加了一条显示在“问题”面板中的附加错误消息。您右键单击了项目,并执行了Maven>更新项目…,对吗?您是否也尝试在Window>Preferences:Java>Installed JRE中删除并重新添加JDK?可能JDK已损坏,不在那里,或者在Eclipse读取它后发生了更改(JDK目录名为
C:\Portable
…)。在切换Java版本时,Eclipse使用最匹配的JDK/JRE of Window>Preferences:Java>安装的JRE或使用
--release
选项(请记住,Eclipse有自己的编译器,支持多个Java版本)。非常感谢您的建议。是的,我已经尝试了所有常见的Eclipse补救方法。虽然我同意您的看法,但这在某种程度上是一个配置问题(maven命令行中使用相同的代码和jdk,eclipse中使用相同的安装jre和IntelliJ的maven build),我认为这不是一个简单的问题(我在我的机器上复制了你的问题。看起来像Eclipse中的一个bug。
The project was not built since its build path is incomplete.
Cannot find the class file for java.lang.String.
Fix the build path then try building this project
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
The type java.io.FilterOutputStream cannot be resolved. It is indirectly referenced from required .class files