antonio_oreany
antonio_oreany

Reputation: 91

Error:java: invalid source release 11 with --enable-preview (preview language features are only supported for release 15)

While trying to build the project, I am faced with an error:

Error:java: invalid source release 11 with --enable-preview (preview language features are only supported for release 15)

How do I solve this error?

// game template
class A_Main {
    private A_World world = null;

    public A_Main() {
        //start the game with the space shooter asteroid game
        A_Frame frame = new Asteroid_Frame();
        frame.displayOnScreen();

        world = new Asteroid_World();

        world.setGraphicSystem(frame.getGraphicSystem());
        world.setInputSystem(frame.getInputSystem());

        A_GameObject.setPhysicsSystem(world.getPhysicsSystem());
        A_GameObject.setWorld(world);
        A_TextObject.setWorld(world);

        world.init();
        world.run();
    }

    public static void main(String[] args) {
        new A_Main();
    }
}

Upvotes: 3

Views: 10461

Answers (2)

eku-code
eku-code

Reputation: 181

I had this error while running unit tests. I set java 11 for all places of my project, but used openjdk-17.

In Intellij IDEA go to File > Settings > Build, Execution > Compiler > Java compiler. In "Override compiler parameters" delete --enable-preview flag from the command line.

Upvotes: 3

oligofren
oligofren

Reputation: 22923

Java comes in several versions. Java 5 was released about 2004, Java 7 in 2011, Java 8 in 2014 and in recent times Java has seen a new major update about twice a year. A Java compiler can compile earlier versions, but it cannot (usually) compile stuff that has syntax for newer versions (like records in newer Java). One exception to that rule is the --enable-preview flag which allows it to compile stuff that was going through review, but was not finalized, at the time the major version was released.

Your error means that the combination of java source version and preview flag does not make sense. This flag always only makes sense with the latest source version the compiler knows. So for JDK 15, that means source code level 15. You have set 11.

A way to fix this is to remove the "preview flag" in the source code version settings in your build tool (Eclipse, Intellij, Maven, Gradle or whatever). As far as I can see, you only use pretty old/classic Java, so it should be buildable by almost anything >= Java 5. Since you have not told us what kind of tooling you use to build your project we cannot help you with exact steps without more information.

You can read more about the preview flag at Baeldung.

Upvotes: 4

Related Questions