2

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?

Thomas
  • 87,414
  • 12
  • 119
  • 157
  • 3
    You got that error because you are compiling with source level 7. You fix it by compiling with source level 8 or higher. That means that you need to be using a Java 8 or higher JDK. But the fact that the Java compiler recognizes the lambda expressions at all means that you must be compiling using JDK 8 or higher compiler. – Stephen C Sep 01 '21 at 06:57
  • You are already compiling with Java 8 or newer. What's the reason you're using 1.7 as source version? – ernest_k Sep 01 '21 at 06:58
  • This seems to be your IDE setting the project's source level to Java 7. Which one are you using? Are you using some build tool like Maven to define the project? – Thomas Sep 01 '21 at 07:03
  • BTW what IDE did you use – Yeshwin Verma Sep 01 '21 at 07:04
  • (jmonkeyengine SDK is based on NetBeans I think) – Stephen C Sep 01 '21 at 07:09

2 Answers2

2

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);
        }
    });
}
1

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

Paul Verest
  • 60,022
  • 51
  • 208
  • 332