Search code examples
javaeclipsegenericsjavac

Java generics code compiles in eclipse but not in command line


I know there have been several questions in the past regarding things that compile in eclipse but not in command line, but I could not find an answer to my problem yet.

In particular, I think that I was able to set eclipse to use my system compiler, but that still did not solve the problem.

I am currently checking under : 'Preferences -> Java -> Installed JREs'.

This contains only one JRE which is my system one.

Here are the specifics of the problem

I have a java generic class that takes as argument an Enum type, like so:

public class MyClass<T extends Enum<T>>

Somewhere inside the class I compare a known enum value with values of T. So for example let's assume I have this enum:

public enum OtherEnum{
 a,
 b
}

And then I test:

protected void foo(T enumVal){
    if(enumVal == OtherEnum.a){
        // do something
    }
    else if(enumVal == OtherEnum.b){
        // do something else
    }
}

This compiles with no problem in eclipse but in command line javac I get this error:

incomparable types: T and OtherEnum

I tried this on two systems that use variant of java 1.6 (1.6.0_26 and 1.6.0_16). One is a Mac, the other a linux. They both give the same error, while eclipse is compiling away with no problem.

So:

  1. How do I make sure which compiler eclipse is using?

  2. What is even the problem here?

Thanks!


Solution

  • Separate compilers...The use of == forces a stricter compile time check, therefore in the CL it is enforcing type comparison and erroring. In Eclipse you can manage the settings for the compile time and loosen the restrictions so to speak.

    You can try Enum.equals() and it may not enforce such strict compile time type comparison. just a thought.