java 为什么 Maven 会生成此错误:“...在 -source 1.5 中不受支持”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10586384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:41:12  来源:igfitidea点击:

Why is Maven generating this error: "...is not supported in -source 1.5"?

javamaven

提问by HolySamosa

This morning Maven starts complaining with this error:

今天早上 Maven 开始抱怨这个错误:

error: multi-catch statement is not supported in -source 1.5

error: multi-catch statement is not supported in -source 1.5

Oddly, I'm using JDK 7 and this code has been building fine for weeks. I'm just using m2e with a default POM with no compiler versions specified.

奇怪的是,我使用的是 JDK 7 并且这段代码已经构建好几个星期了。我只是在没有指定编译器版本的情况下使用带有默认 POM 的 m2e。

Here's my Maven version information:

这是我的 Maven 版本信息:

Apache Maven 3.0.2 (r1056850; 2011-01-08 19:58:10-0500)
Java version: 1.7.0_03, vendor: Oracle Corporation
Java home: C:\SDKs\Java\jdk1.7.0_03\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"

I can get around it by using the Maven compiler plugin like so:

我可以通过使用 Maven 编译器插件来解决它,如下所示:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>

Still, I'd like to understand why Maven would suddenly start misbehaving and requiring the use of the compiler plugin.

不过,我想了解为什么 Maven 会突然开始行为不端并需要使用编译器插件。

采纳答案by dsummersl

Most likely this is a problem with your environment, not maven (ie, your JAVA_HOME environmental variable changed). It's worth noting that the compiler plugin is required anyway. By including this compiler section in your pom you are ensuring that your code gets compiled with the correct compiler, regardless of your environmental settings.

这很可能是您的环境问题,而不是 maven(即,您的 JAVA_HOME 环境变量已更改)。值得注意的是,无论如何都需要编译器插件。通过在 pom 中包含此编译器部分,您可以确保使用正确的编译器编译您的代码,而不管您的环境设置如何。

回答by Dave Newton

The default source/target levels are 1.5, which doesn't support Java 1.7 syntax, obviously.

默认的源/目标级别是 1.5,这显然不支持 Java 1.7 语法。

As to why it would "suddenly" change, something changed in your m2e or project configuration.

至于为什么它会“突然”改变,你的 m2e 或项目配置发生了一些变化。