6

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?

doc
  • 765
  • 1
  • 6
  • 24
  • Maybe you missed to configure the JDK in _Window > Preferences: Java > Installed JREs_. Otherwise, look at the project (build) problems Eclipse tells you. The root cause is a misconfiguration of the project or of Eclipse, not that String cannot be resolved. – howlger Jun 11 '20 at 13:38
  • The JDK is there. I've added an additional error message shown in the problems panel. – doc Jun 11 '20 at 13:51
  • You right-clicked the project and did _Maven > Update Project..._, right? Did you also try to remove and re-add the JDK in _Window > Preferences: Java > Installed JREs_? Maybe the JDK is broken, was not there or changed after Eclipse read it (the JDK directory is called `C:\Portable`...). When switching the Java version, Eclipse uses the best matching JDK/JRE of _Window > Preferences: Java > Installed JREs_ or uses the `--release` option (keep in mind, Eclipse has its own compiler that supports multiple Java versions). – howlger Jun 11 '20 at 15:12
  • Thank you for very much your suggestions.Yes, I have tried all the usual Eclipse remedies like that. Whilst I agree with you it is somehow a config issue (the same code and jdk work in maven command line and maven build in eclipse with the same installed jre and IntelliJ), I don't think it is a straightforward one. :( – doc Jun 12 '20 at 06:40
  • I reproduced your issue on my machine. Looks like a bug in Eclipse. – ZhekaKozlov Jun 13 '20 at 10:59
  • I just tried with Eclipse 2019-03 and it works fine. I've filed a bug report: https://bugs.eclipse.org/bugs/show_bug.cgi?id=564289 – doc Jun 15 '20 at 08:36

4 Answers4

4

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.

Fred Bricon
  • 5,369
  • 1
  • 31
  • 45
1

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

0

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.

Park JongBum
  • 1,245
  • 1
  • 16
  • 27
-1

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.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103