5

I have encountered some java8-related code in a maven project that compiles with the javac compiler, but gives compilation-errors in Eclipse (ECJ compiler differ from javac, I suppose).

I'm importing it in Eclipse-Luna with: Import => Maven => Existsing Maven Project

As a quick fix, is there a way to make Eclipse use javac in a maven-project (thus disabling the ECJ-compiler)?

EDIT: Adding a minimal poc-example of compiler-diff.

This code compiles with javac, but the List-initialization gives error in Eclipse: "The target type of this expression must be a functional interface"

package test;

import static java.util.Arrays.asList;

import java.util.List;

public class Test01 {

    private static final List<MyInterface> items = asList(() -> "123", () -> "456");

    public void test01() {
        System.out.println("Hello");
    }

    public interface MyInterface {
        String value();
    }
}

The error disappears if you add typecasts:

private static final List<MyInterface> items = asList((MyInterface) () -> "123", (MyInterface) () -> "456");
Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
Rop
  • 3,359
  • 3
  • 38
  • 59
  • I think it would be better if you show us some code and tell us the compiler errors you see - I suspect there is a better solution here than trying to get Eclipse to use something other than its own compiler. I would imagine it's far more likely that the issue is configuration, rather than compiler problems. – Duncan Jones Aug 28 '14 at 09:02
  • I can change the code, and make it compile, no problem. But since others are using intellij (with different compiler) in the same project, this issue keeps popping up, and I then have to modify the code in each such instance. That's why I would prefer to use javac, to ensure compatibility. Basically, everything that builds on maven command line, need to build in Eclipse, too. – Rop Aug 28 '14 at 09:08
  • I completely understand that. But it seems you've jumped to the conclusion that the Eclipse compiler is at fault. Perhaps the configuration of your Eclipse is wrong in another way. If you provide the information I've suggested, we can help solve your problem rather than help you with your idea for a solution. See [What is the XY problem?](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) to understand what I mean. – Duncan Jones Aug 28 '14 at 09:10
  • OK, thanks for helping, Duncan. I added a simple poc in my question. – Rop Aug 28 '14 at 09:34
  • 1
    Looks like you've stumbled into [this Eclipse bug](https://bugs.eclipse.org/bugs/show_bug.cgi?id=437007). The good news is they've fixed it. Perhaps you can download a nightly-build or something for the time being until the fix hits the next update? – Duncan Jones Aug 28 '14 at 09:51
  • Ok, so *still* would be good if there was a way to switch to javac-compiler in Eclipse :) while waiting for bugfixes, etc... :) – Rop Aug 28 '14 at 09:58
  • 2
    Indeed, I'll stop chattering now. Only to point out that SR1 is [due for release end of September](http://wiki.eclipse.org/Luna/Simultaneous_Release_Plan#SR1) and I think that will contain your fix. In the meantime, [check out this answer](http://stackoverflow.com/a/22402358/474189) which should help you. – Duncan Jones Aug 28 '14 at 10:01
  • There is no way to switch compiler inside Eclipse, as the Eclipse compiler is an integral part of having Eclipse understand your java sources as more than just plain text. – Thorbjørn Ravn Andersen Jun 04 '15 at 08:06

0 Answers0