Eclipse jdk 11 problem: The type java.lang.String cannot be resolved. It is indirectly referenced from required .class files

Viewed 9296

I have a toy program that has a compilation error only in Eclipse when I try to use a Chronicle import and compile to language level 11. The program compiles and runs in maven, and also in IntelliJ (with the same maven and JDK).

The versions I have are:

  • maven 3.6.1
  • jdk openjdk version "11" 2018-09-25
  • eclipse 2020-03-R
  • chronicle-bom 2.19.199 (supported java11 since 2.17)

This is my 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>

And here is my simple test class:

import net.openhft.chronicle.bytes.BytesMarshallable;

public class App {
    public static void main(String[] args) {
        System.out.println("BytesMarshallable: " + new BytesMarshallable() {});
    }
}

The output when running exec:java directly with maven is

[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] ------------------------------------------------------------------------

It compiles in IntelliJ and this is the output when I run as a Java Application:

BytesMarshallable: App$1@39fb3ab6

Process finished with exit code 0

However, in Eclipse the class App will not compile. The error is on the import line and says:

The type java.lang.String cannot be resolved. It is indirectly referenced from required .class files

There is an additional message in the Problems panel:

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

But I don't see what the issue is in my build path:

enter image description here

Furthermore, I can see java.lang is present in the package explorer:

enter image description here

(Note that if I change the language level to 8, but still using JDK 11, it will work in Eclipse.)

I have checked for obvious issues (build path, maven/jdk path) and everything appears correct to me. Why do I get this error in Eclipse and how can I fix it?

4 Answers

The full error message is :

  • Type java.lang.String is indirectly referenced from required .class files but cannot be resolved since the declaring package java.lang exported from module java.base conflicts with a package accessible from module

It's caused by one of the transitive dependencies, net.openhft:affinity:3.2.3, embedding 2 classes from the java.lang package, which is illegal. The ECJ compiler in Eclipse complaining about it is expected. However, the fact it works in javac is a bug in itself: https://bugs.openjdk.java.net/browse/JDK-8215739

There's an affinity issue about it: https://github.com/OpenHFT/Java-Thread-Affinity/issues/58

If you're not using the thread affinity features, just exclude affinity from your dependencies and the Eclipse compiler will stop complaining.

In my case I had following error, and it had no relationship with "Configure build path" --

The type java.io.FilterOutputStream cannot be resolved. It is indirectly referenced from required .class files

enter image description here

The error was solved after I create a package and located the problematic class into the class.

The error was solved after I opened Window > Preferences: Java > Installed JREs > Execution Env And re-selected the JavaSE-11, Apply and Close

It has a mismatch with pom file so update in pom file as well.

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

I have used these two then its working fine for me.

Related