【问题标题】:Why can't java 7 use diamond operator and multi-catch statementjava 7为什么不能使用菱形运算符和multi-catch语句
【发布时间】:2016-03-15 15:22:03
【问题描述】:

嗯,使用 Java 7 (1.7.0_67) 并且项目语言级别设置为 7-Diamonds、ARM、multi-catch。我的代码如下,使用maven构建时抛出编译错误的行。

private Map<String, List<InstrumentationClassData>> classMap = new HashMap<>(); //line 36 in InstrumentingAgent

InstrumentingAgent 第 63 行中的多捕获块

} catch (InstrumentationException | JAXBException e){
            e.getMessage();
}

编译时出现以下错误。为什么它不起作用?我究竟做错了什么。我正在开发 IntelliJ IDE。

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project instrumentation-agent: Compilation failure: Compilation failure:
[ERROR] /home/Documents/instrumentation-agent/src/main/java/org/wso2/das/javaagent/instrumentation/InstrumentingAgent.java:[36,79] error: diamond operator is not supported in -source 1.5
[ERROR] (use -source 7 or higher to enable diamond operator)
[ERROR] /home/Documents/instrumentation-agent/src/main/java/org/wso2/das/javaagent/instrumentation/InstrumentingAgent.java:[63,47] error: multi-catch statement is not supported in -source 1.5
[ERROR] (use -source 7 or higher to enable multi-catch statement)

根据我阅读的内容,菱形运算符应该适用于 Java 7。但是为什么我会得到这个。如果我用相关类型填充菱形,那么 IDE 会将它们变为灰色并说它可以替换为菱形运算符。但是当我替换它时会出现以下错误。

【问题讨论】:

  • 嗯,使用 Java 7 不,你不是。您正在使用 1.5 进行编译。
  • 错误消息说您正在编译为 1.5。如果您搜索如何让 maven 使用 java 7 进行编译,那里有各种资源,应该可以为您解决这个问题。
  • 您还使用了旧版本的 maven-compiler-plugin,这表明您的 POM 中根本没有插件配置。
  • 我认为您将 IDE 中运行的 Java 版本(您 看到的)与您用于 _build 的 Java 版本(导致错误的原因)混淆了。只需将您的构建 JDK 更改为 7 或更高版本,一切都应该可以正常编译。
  • 感谢您的快速回复。当我将上述部分添加到 pom 并编译时,它没有任何错误。

标签: java maven intellij-idea


【解决方案1】:

在 pom 中添加以下内容解决了编译错误,

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.2</version>
    <configuration>
        <source>1.7</source>
        <target>1.7</target>
    </configuration>
</plugin>

【讨论】:

    猜你喜欢
    • 2014-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多