"Lambda expressions are not supported in -source 7, use -source 8 to enable lambda expressions"

2.6k views Asked by At

So I have been programming a bit with java for a while, and I started on making games, as I'm not super experienced with it I decided to make a simple 2D game. It was going pretty good until I met a problem: (can't embed pictures yet, but basically it says as in my title.

1

This is the code I have been using for the Application that received the error:


import java.awt.EventQueue;
import javax.swing.JFrame;

public class Application extends JFrame {
    
    public Application() {
        
        initUI();
    }
    
    private void initUI() {
        
        add(new Board());
        
        setSize(250, 200);
        
        setTitle("Application");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }
    
    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            Application ex = new Application();
            ex.setVisible(true);
        });
    }
}

Anyone know why I got that error and how to fix it?

2

There are 2 answers

0
Paul Verest On

in IDEA File -> Project Structure
select JDK 8+
choose language level at least 8.

enter image description here

(Of course you will need to download new JDK if you have no Java 8 capable JDK)

If you use maven or gradle, you change in pom.xml (see below) and update project settings from pom.xml

enter image description here

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- Your dependencies -->
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>

For gradle see docs, as it changes often with newer gradle versions https://docs.gradle.org/current/userguide/building_java_projects.html

0
AudioBubble On

You can rewrite the lambda expression like this:

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            Application ex = new Application();
            ex.setVisible(true);
        }
    });
}