[java] Java "lambda expressions not supported at this language level"

I was testing out some new features of Java 8 and copied the example into my IDE (Eclipse originally, then IntelliJ) as shown here

Eclipse offered no support whatsoever for lambda expressions, and IntelliJ kept reporting an error

Lambda expressions not supported at this language level

I would like to know if this is a problem with my install, the code, or support.

This question is related to java intellij-idea lambda java-8

The answer is


Adding below lines in app level build.gradle in Android project solved issue.

 compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }

To compile with Java 8 (lambdas etc) in IntelliJ IDEA 15;

  • CTRL + ALT + SHIFT + S or File / Project Structure

  • Open Project tab

  • Set Project SDK to compatible java version (1.8)

  • Set Project language level to 8

  • Open Modules tab

  • Set Language level to 8, click OK

  • CTRL + ALT + S or File / Settings

  • Build, Execution, Deployment / Compiler / Java Compiler

  • Change Target bytecode version to 1.8, click OK


For latest users using Android studio 3.0.1 or above. Simply click Alt + Enter and upgrade your language level to 8.0 - Lambdas, type annotations etc.


Ctrl + Alt + Shift + S and then from project select the language level 1.8, read more about Lambda Expressions here


Just add compileOptions in build.gradle yours app:

android {


...

  defaultConfig {
    ...
    jackOptions {
      enabled true
    }
  }
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

Actually, Eclipse support for Java 8 is available: see here and here.

  • For Kepler, it is available as a "feature patch" against SR2 (i.e. Eclipse 4.3.2)
  • For Luna (Eclipse 4.4) it will be in the standard release. (release schedule)

In Android studio canary build(3.+), you can do the following:

File -> Project Structure -> Look in Modules Section, there are names of modules of your class. Click on the module in which you want to upgrade java to 1.8 version. -> Change "Source Compatibility" and "Target Compatibility" to 1.8 from 1.7 or lower. Do not change anything else. -> Click Apply

now gradle will re-sync and your error will be removed.


If you are getting Not supported at Language 5, check this: ( For me it was in the Modules section)

Lanaguge Level 5 not supported


7.0 does not support lambda expressions. Just add this to your app gradle to change your language level to 8.0:

compileOptions {
    targetCompatibility 1.8
    sourceCompatibility 1.8
}

enter image description here


First check the version of JDK you are using, it should higher than 1.8.

If it is then proceed to do below steps.

Change Language level of Project and modules in Intellij.

Project

1.File->Project Structure->Project->Project language level Change it to 8 for lamba expressions to work

2.File->Project Structure->Module->Project language level Change it to 8 for lamba expressions to work


Possible Cause #1 (same as the first 3 answers for this question): Project Structure Settings

  1. File > Project Structure > Under Project Settings, go to Project > set Project SDK to 1.8 (with version 65 or above, latest is 112) and set Project Language Level to 8.

  2. File > Project Structure > Under Platform Settings, go to Project > ensure that your JDK home path is set to 1.8 (with version 65 or above, latest is 112).

If the above does not work, check the target bytecode version of your compiler and move on to Possible Cause #2.

Possible Cause #2: Compiler

  1. File > Settings > Build, Execution, Deployment > Under Compiler, go to Java Compiler > set your Project Bytecode Version to 1.8, as well as all the Target Bytecode Version for your modules. Click Apply and hit OK. Restart your IntelliJ IDE. Lambda expression was accepted in my IDE at this point.

Best wishes :)


For intellij 13, Simply change the Project language level itself to 8.0 with following navigation.

File
|
|
 ---------Project Structure -> Project tab
          |
          |________Project language level

java8

Module lang level

I also had to update Modules lang level when there was no maven plugin for java compiler.

File
|
|
 ---------Project Structure -> Modules tab
          |
          |________ language level

Modules lang level

But this Module lang level would automatically be fixed if there's already a maven plugin for it,

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

After changes everything looks good


The same project (Android Studio 3.3.2, gradle-4.10.1-all.zip, compileSdkVersion 28, buildToolsVersion '28.0.3') works fine on the new fast Windows machine and underline Java 8 stuff by red color on the old Ubuntu 18.04 laptop (however project is compiling without errors on Ubuntu).

The only two things I have changed to force it to stop underlining by red were excluding of incremental true and dexOptions

compileOptions {
//    incremental true
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

//dexOptions {
//    javaMaxHeapSize "4g"
//}

in the app- level build.gradle.


For me none of the solutions provided worked.

This should be your last resort: -> Open .iml file of your project change the LANGUAGE_LEVEL to JDK_1_8 or whatever version you wish. -> If this didn't help, you should grep "JDK" in your project root directory and find the file which contains version setting and set it to your preferred version.


this answers not working for new updated intelliJ... for new intelliJ..

Go to --> file --> Project Structure --> Global Libraries ==> set to JDK 1.8 or higher version.

also File -- > project Structure ---> projects ===> set "project language manual" to 8

also File -- > project Structure ---> project sdk ==> 1.8

This should enable lambda expression for your project.


Check below settings:

  1. File->Project Structure->Project Tab.

    *Project SDK should be set to 8 or above.*
    
  2. File->Project Structure->Modules.

    *Check that your or all modules has Language level set to 8 or above.*
    
  3. File->Settings(or Other Settings->Preference for new projects in newer Idea version)

    *Search for Java compiler and check if Project byte code version 
    and per module byte code version is set 8 or above.*
    

You problem may fall into any one of the category defined above.


You should change source code Language Level also on the Source tab (Modules part).

Change Language Level


As Stephen C pointed out, Eclipse Kepler (4.3) has Java 8 support when the patch is installed (installation instructions here)

Once installed, you’ll need to tell your projects to use java 8. First add the JDK to eclipse:

  • Go to Window -> Preferences
  • Go to Java -> Installed JREs
  • Add Standard VM, and point to the location of the JRE
  • Then go to Compiler
  • Set Compiler compliance level to 1.8

Then tell the project to use JDK 1.8:

  • Go to Project -> preferences
  • Go to Java Compiler
  • Enable project specific settings
  • Set Compiler compliance level to 1.8

This solution works in Android Studio 3.0 or later.

  1. File > Project Structure > Modules > app > Properties tab

enter image description here

Change both of Source Compatibility and Target Compatibility to 1.8

  1. Edit config file

You can also configure it directly in the corresponding build.gradle file

android {
  ...
  // Configure only for each module that uses Java 8
  // language features (either in its source code or
  // through dependencies).
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

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> 

Using: IntelliJ IDEA 2017.1.4 Build #IU-171.4694.23

Changing the language level via the Project Settings / Project tab didn't do anything. Instead hitting Alt + Enter and selecting "Set language level to 8 - Lambdas, type annotations etc." and it resolved automatically.


To have support of Ecplise for lambda expressions :

  1. Make sure you are using JDK 1.8 (Window > Preferences > Installed JREs and Compiler );
  2. Use the annotation @FunctionalInterface if you define any new functional interfaces so that eclipse will suggest you refactorings related to Lambdas ( Functional Interfaces. )

In intellij 14, following settings worked for me. Intellij14 Settings


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to intellij-idea

IntelliJ: Error:java: error: release version 5 not supported Has been compiled by a more recent version of the Java Runtime (class file version 57.0) Error: Java: invalid target release: 11 - IntelliJ IDEA IntelliJ can't recognize JavaFX 11 with OpenJDK 11 Error: JavaFX runtime components are missing, and are required to run this application with JDK 11 ERROR Source option 1.5 is no longer supported. Use 1.6 or later Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6 How to configure "Shorten command line" method for whole project in IntelliJ intellij idea - Error: java: invalid source release 1.9 Failed to resolve: com.google.android.gms:play-services in IntelliJ Idea with gradle

Examples related to lambda

Java 8 optional: ifPresent return object orElseThrow exception How to properly apply a lambda function into a pandas data frame column What are functional interfaces used for in Java 8? Java 8 lambda get and remove element from list Variable used in lambda expression should be final or effectively final Filter values only if not null using lambda in Java8 forEach loop Java 8 for Map entry set Java 8 Lambda Stream forEach with multiple statements Java 8 stream map to list of keys sorted by values Task.Run with Parameter(s)?

Examples related to java-8

Default interface methods are only supported starting with Android N Class has been compiled by a more recent version of the Java Environment Why is ZoneOffset.UTC != ZoneId.of("UTC")? Modify property value of the objects in list using Java 8 streams How to use if-else logic in Java 8 stream forEach Android Studio Error: Error:CreateProcess error=216, This version of %1 is not compatible with the version of Windows you're running Error:could not create the Java Virtual Machine Error:A fatal exception has occured.Program will exit What are functional interfaces used for in Java 8? java.time.format.DateTimeParseException: Text could not be parsed at index 21 Java 8 lambda get and remove element from list