IntelliJ: evaluate lambda expression raises an com

2019-04-21 07:16发布

I'd like to evaluate code including lambda expression with Intellij 'Evaluate Code Fragment' feature. But then, Intellij raises an error Unable to compile for target level 1.8. Need to run IDEA on java version at least 1.8, currently running on 1.6.0_65-b14-462-11M4609

The evaluating code is very simple as below.

Set<Integer> set = new HashSet<>();
set.add(1);
set.stream().map(v->v).collect(Collectors.toSet());

My Intellij version is 14.0.3 and according to official document, version 14 supports lambda expression evaluation.

How can the feature be available?

enter image description here

4条回答
Summer. ? 凉城
2楼-- · 2019-04-21 07:53

Putting together the comments from adrian lange and Bohuslav Burghardt, either upgrade to the latest IntelliJ 14.1 which comes bundled with Java 1.8, or switch your current IntelliJ to use Java 1.8. See bug IDEA-132099.

To switch to Java 1.8:

  1. Install Java 8
  2. Change IntelliJ to use Java 8. See these instructions.Project structure
查看更多
萌系小妹纸
3楼-- · 2019-04-21 07:54

You can switch runtime version of JDK:

  1. Start the IDE, Help | Find Action (Ctrl+Shift+A or Cmd+Shift+A on Mac), type "Switch IDE Boot JDK", press Enter.
  2. Start the IDE, use Help | Find Action (Ctrl+Shift+A or Cmd+Shift+A on Mac), type "Switch IDE Boot JDK", press Enter. Switch IDE Boot JDK dialog appears. Select the version from the drop-down or click "…" entry to specify the custom location.
查看更多
别忘想泡老子
4楼-- · 2019-04-21 08:01

Even after applying above defined project specific settings on IntelliJ as well as Eclipse, it was still failing for me !

what worked for me was addition of maven plugin with source and target with 1.8 setting in POM XML:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.0.2</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>com.abc.sparkcore.JavaWordCount</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy</id>
                    <phase>install</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build> 
查看更多
爷、活的狠高调
5楼-- · 2019-04-21 08:09
Unable to compile for target level 1.8...Currently running on version 1.6

My guess is that your project is being run on a version of Java that doesn't support lambdas. Make sure you have Java 8 installed, and also make sure to change your project level to use Java 8, like so: Notice that it explicitly mentions lambdas

查看更多
登录 后发表回答