java - 为什么这段代码没有用 javac 编译但在 eclipse 中没有错误?

标签 java eclipse compiler-construction javac

以下代码:

@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 {};
}

在 eclipse 中编译但在 sun/oracle 编译器中编译失败:

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

发生这种情况是因为 MinTimeDoubleCoListConstraintValidator.class, 之后的逗号。

当我删除逗号时它工作正常:

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

我正在使用 jdk 1.6.0.10。
你知道为什么这是非法的并且在 eclipse 中编译吗?

最佳答案

这是 Java 6 的 javac 中的错误。 The JLS allows trailing commas在某些地方,Eclipse 编译器遵循此处的标准,而 Java 6 从不允许在任何地方使用尾随逗号。

您可以尝试使用 Java 7 中的 javac 选项编译您的代码 -source 6 -target 6(以获得 Java 6 兼容字节码)。如果错误仍然存​​在,file it .它可能会得到修复。

关于java - 为什么这段代码没有用 javac 编译但在 eclipse 中没有错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10380839/

相关文章:

java - 是否可以使C/C++平台无关

java - 对 Java 中的克隆感到困惑

java - 过滤器 servlet 不适用于 Netbeans 中的 glassfish

eclipse - 更改 Eclipse 中的事件项目 - SVN 同步

java - 为什么我不能从 Java 中的专用枚举值访问静态最终成员

c - 转编译为另一种语言

Java程序跳过while循环

android - 构建工作空间,构建过程中发生错误

eclipse - Eclipse 中到处都是未知符号

compiler-construction - 编程语言的标准库是如何实现的?