230

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.

Asad Ali Choudhry
  • 4,985
  • 4
  • 31
  • 36
Czipperz
  • 3,268
  • 2
  • 18
  • 25
  • 3
    It's an IDE issue. You have to change your compiler compliance. – Sotirios Delimanolis Mar 28 '14 at 03:06
  • 6
    In IDEA, select File -> Project structure; you'll have the option to change the language level – fge Mar 28 '14 at 03:06
  • 1
    @Czipperz: On SO don't annotate the title with "Solved", mark the best answers as "Accepted" by clicking the tick. That rewards the answerer and shows the question has an answer which resolves the problem. If no answer gives the right resolution, you are free to post a self-answer and then mark it accepted (after a waiting period). – Lawrence Dol Mar 28 '14 at 03:23
  • possible duplicate of [How do I use Java 8 features in Eclipse Luna?](http://stackoverflow.com/questions/22668739/how-do-i-use-java-8-features-in-eclipse-luna) – Svetlin Zarev Mar 28 '14 at 07:57
  • @LawrenceDol I annotated it like that because they didn't post it as an answer, but as a comment. – Czipperz Mar 28 '14 at 20:36
  • Well this is for IntelliJ as well, but thanks for the support for Eclipse @SvetlinZarev – Czipperz Mar 28 '14 at 20:37
  • do you use some kind of build tools? (maven, etc.) - the preferred setup might be different – voho Nov 06 '15 at 08:49
  • *"Eclipse offered no support whatsoever for lambda expressions"*- This is patently incorrect, and was incorrect when the question was asked; see my answer and others that tell you how to get it. – Stephen C Mar 23 '19 at 23:41

25 Answers25

400

In IntelliJ IDEA:

In File MenuProject StructureProject, change Project Language Level to 8.0 - Lambdas, type annotations etc.

For Android 3.0+ Go FileProject StructureModuleapp and In Properties Tab set Source Compatibility and Target Compatibility to 1.8 (Java 8)

Screenshot:

enter image description here

Asad Ali Choudhry
  • 4,985
  • 4
  • 31
  • 36
Abhilash Divakaran
  • 4,229
  • 1
  • 16
  • 16
  • 71
    **Note from proposed edit**: You can also configure language level for every module, `File Menu → Project Structure → Modules`, but by default it will the same as Project language level. – user1516873 May 14 '15 at 16:18
  • 6
    I'm facing same issue for Android Studio and neither found same settings in IDE you mention above. Any suggestion ! – CoDe Sep 02 '15 at 06:49
  • 19
    @Shubh: In Gradle, add these lines, your code will work: `android { compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } }` – BaDo Sep 25 '15 at 02:30
  • 7
    After **upgrading** to **IntelliJ 15**, I had to change the *Project Language Level* up to 9 and then back to 8 for that to take effect. – averasko Nov 03 '15 at 14:29
  • 2
    In maven, you have to explicitly set to 1.8 language level. http://stackoverflow.com/questions/16723533/how-do-you-specify-the-java-compiler-version-in-a-pom-xml-file – mjlee Jan 10 '16 at 16:32
  • 3
    does not exist on AS1.5.1 on Mac – Jemshit May 13 '16 at 12:48
  • I was facing the same issue but it was already on 8.0. The comment from @user1516873 helped me solve the issue – Ced Jun 21 '16 at 22:51
  • 18
    This answer is outdated. On Android 3.1.2 go to ```File``` -> ```Project Structure``` -> ```Module you want to support lambdas``` and set **Source Compatibility** and **Target Compatibility** to **1.8** – Ivan May 18 '18 at 20:25
  • @Ivan yes thank you for the updated version, I've been searching for so long, [this is](https://developer.android.com/studio/intro/studio-config#jdk) the same thing but not UI – Roshna Omer Sep 06 '18 at 07:20
  • 1
    In Android Studio, it's File--Project Structure, select App under Module, Properties Tab, then set Source Compability to 1.8. Oh already mentioned above by Ivan – GeoffL Mar 19 '19 at 10:14
  • On Android Studio 3.6.3, my screen showed 1.8 supported but I was getting the compiler error. I read @averasko comment and switched my setting to 1.7, applied, then switched back to 1.8 and applied, and the compile error went away. Thanks! – Mike Aug 05 '20 at 15:06
106

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

Change Language Level

Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
dmitry.bogatko
  • 1,209
  • 1
  • 8
  • 4
  • 3
    your answer should be higher or together with other answers – Anton Krug Mar 01 '16 at 14:42
  • 2
    +1. The priority of modules is higher than the project's. So I prefer setting the language level of modules part. – Ge Liu May 06 '16 at 14:56
  • This doesn't include the module which IntelliJ also requires. Not crazy about this side of IntelliJ, too many places to check the same thing. – SpacePrez Sep 19 '17 at 21:36
  • Awesome. Thank you so much. This saved my day. I was struggling with the error not sure what needs to be done to enable the lambda expressions. – Sidd Thota Oct 24 '17 at 22:27
  • I was running a spring boot ap, and not sure what went wrong after I started using lambda expressions, I changed the project settings, but after following this modules setting, It worked fine. – Sidd Thota Oct 25 '17 at 00:13
  • Thanks alot for screen shot, I was wondering as I have Language Level 8 already selected in File Menu → Project Structure → Project, change Project Language Level and it was not working. Your screen shot save my day! – Fahad Dec 08 '17 at 05:16
80

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
  }
}
Hasan El-Hefnawy
  • 1,249
  • 1
  • 14
  • 20
Alen Lee
  • 2,479
  • 2
  • 21
  • 30
45

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

prayagupa
  • 30,204
  • 14
  • 155
  • 192
24

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.

Tarun Deep Attri
  • 8,174
  • 8
  • 41
  • 56
20

Just add compileOptions in build.gradle yours app:

android {


...

  defaultConfig {
    ...
    jackOptions {
      enabled true
    }
  }
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}
Morozov
  • 4,968
  • 6
  • 39
  • 70
  • 3
    Google announced that they deprecating Jack a few weeks ago. Java 8 language features support is now natively provided by the Android Gradle plugin, starting with Android Studio 2.4 Preview 4. Look [here](https://issuetracker.google.com/issues/37126573#comment2) – nilsi Apr 20 '17 at 06:18
10

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.

Vivek
  • 165
  • 2
  • 12
7

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

Lanaguge Level 5 not supported

Kejsi Struga
  • 550
  • 6
  • 21
5

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

live-love
  • 48,840
  • 22
  • 240
  • 204
2

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)
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
2

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

Daniel de Zwaan
  • 3,064
  • 25
  • 24
2

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.

Patrick Schaefer
  • 821
  • 10
  • 20
1

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
Gijs Overvliet
  • 2,643
  • 3
  • 28
  • 35
1

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.

1

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.

isabsent
  • 3,683
  • 3
  • 25
  • 46
0

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

Manjunath Ballur
  • 6,287
  • 3
  • 37
  • 48
0

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> 
Dean Jain
  • 1,959
  • 19
  • 15
0

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 :)

kuzevni
  • 103
  • 2
0

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.

0

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

 compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
Tarun Anchala
  • 2,232
  • 16
  • 15
0

File > Project Structure > Modules > Source tab> Change Language level to 8 enter image description here

Abd Abughazaleh
  • 4,615
  • 3
  • 44
  • 53
-1

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

Toseef Zafar
  • 1,601
  • 4
  • 28
  • 46
-1

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. )
Mehdi
  • 1,340
  • 15
  • 23
-1

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

Roshna Omer
  • 687
  • 1
  • 11
  • 20
-1

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.

Abhijit
  • 333
  • 4
  • 7