javaeclipsejavac

why is this code not compiling with javac but has no errors in eclipse?


the following code:

@Retention(RetentionPolicy.RUNTIME)
@Target( { ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE })
@Constraint(validatedBy = {
        MinTimeIntCoConstraintValidator.class, 
        MinTimeIntCoListConstraintValidator.class,
        MinTimeDoubleCoConstraintValidator.class, 
        MinTimeDoubleCoListConstraintValidator.class,
        })
@Documented
public @interface MinTimeValueCo
{
    int value();
    String message() default "value does not match minimum requirements";
    Class<?>[] groups() default { };
    Class<? extends Payload>[] payload() default {};
}

compiled in eclipse but fails to compile in sun/oracle compiler:

> MinTimeValueCo.java:19: illegal start of expression
>     [javac]       })
>     [javac]       ^
>     [javac] 1 error

This happened because of the comma after MinTimeDoubleCoListConstraintValidator.class,.

when I removed the comma it works fine:

@Constraint(validatedBy = {
        MinTimeIntCoConstraintValidator.class, 
        MinTimeIntCoListConstraintValidator.class,
        MinTimeDoubleCoConstraintValidator.class, 
        MinTimeDoubleCoListConstraintValidator.class
        })

I am using jdk 1.6.0.10.
Do you know why this is illegal and compiling in eclipse?


Solution

  • This is a bug in Java 6's javac. The JLS allows trailing commas in some places and the Eclipse compiler follows the standard here while Java 6 never allows trailing commas anywhere.

    You can try to compile your code with javac from Java 7 with the options -source 6 -target 6 (to get Java 6 compatible byte code). If the bug is still there, file it. It might get fixed.