Error compiling code with lambda expression

I have the following code:

package com.mongoDB; import spark.Spark; public class HelloWorldSparkStyle { public static void main(String[] args) { Spark.get("/hello", (req, res) -> "Hello World"); } } 

It works fine when I run it using the main method, but when I try to compile it, it produces the following error:

 \HelloWorldSparkStyle.java:[9,33] error: lambda expressions are not supported in -source 1.5 D:\WorkspaceWithJava8\BeginnerProject>javac -version javac 1.8.0_60 

I am using the Eclipse IDE and trying to compile it through the command line.

+6
source share
2 answers

By default, maven-compiler-plugin uses Java 5 to compile classes. Quoting his documentation :

Also note that the default source parameter is currently 1.5, and the default target destination parameter is 1.5, regardless of the JDK with which you run Maven. If you want to change these defaults, you must set the source and target, as described in "Configuring -source and -target of the Java Compiler".

You need to configure it to use Java 8, for example:

 <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> 
+6
source

Let me elaborate on the answer. Put the plugin in the answer above between <project> and </project> , as shown below.

 <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> 
+1
source

All Articles