【发布时间】:2015-10-28 16:30:51
【问题描述】:
我正在使用 Maven 和 Java Spark 构建一个简单的 Web 服务。
我正在运行 Java 8。
我的(非常简单的)服务:
package com.mycompany.app;
import static spark.Spark.*;
public class HelloWorld {
public static void main(String[] args) {
get("/hello", (req, res) -> "Hello World");
}
}
pom.xml 文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.simplerelevance.app</groupId>
<artifactId>art2</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>art2</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
</project>
当我尝试运行mvn compile 时,我的错误是:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.0.2:compile (default-compile) on project art2: Compilation failure: Compilation failure:
[ERROR] /home/pierceg/SR_Repos/ART2/art2_java/art2/src/main/java/com/simplerelevance/app/HelloWorld.java:[3,7] error: static import declarations are not supported in -source 1.3
[ERROR]
[ERROR] (use -source 5 or higher to enable static import declarations)
[ERROR] /home/pierceg/SR_Repos/ART2/art2_java/art2/src/main/java/com/simplerelevance/app/HelloWorld.java:[7,37] error: lambda expressions are not supported in -source 1.3
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
not supported in -source 1.3 措辞让我觉得我使用的 java 版本有问题。以前,我遇到了同样的错误,但 -source 1.5,所以我将 $JAVA_HOME 更改为 /usr/lib/jvm/java-8-oracle/jre/。这是应该的吗?我的一位同事同意我的观点,即 -source 应该是 1.8。
编辑: mvn -v 返回:
Apache Maven 3.0.5
Maven home: /usr/share/maven
Java version: 1.8.0_51, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-8-oracle/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.16.0-30-generic", arch: "amd64", family: "unix"
【问题讨论】:
-
我以为它默认为 1.5,但无论如何,你没有设置它——尝试设置它,例如,stackoverflow.com/q/10586384/438992
-
您运行的是哪个版本的 Maven?最新版本附带更新版本的编译器插件,默认源兼容性为 1.5
-
最佳实践是始终定义您正在使用的所有插件及其适当的版本。
-
@RoddyoftheFrozenPeas 这就是我所缺少的,非常感谢