In this short article, we will showcase how to solve java: error: invalid source release: 18.

First, we will take a close look at the main causes behind the error. Then, we will explore different ways of fixing it.

IntelliJ IDEA - Java: Error: Invalid Source Release: 18

In short, The Invalid source release error occurs when there is a JDK version mismatch between the project (specified in Maven) and the IntelliJ structure.

In this case, “source release 18” refers to Java 18.

To reproduce the error, let’s choose Java 8 (1.8) as the main JDK of our project in IntelliJ:

Setup IntelliJ JDK

Next, we are going to edit the pom.xml file to specify Java 18 as the value of maven.compiler.source:

Set maven compiler source

Now, if we compile the project using Maven, we will get Invalid source release: 18:

java: error: invalid source release: 18

In a nutshell, the root cause of the error is trying to compile a project using a version of Java that is not supported by the JDK configured in IntelliJ.

So to fix the error, we need to upgrade the project version to JDK 18.

To do so, we can select our project, then navigate to:

    
        File -> Project Structure
                -> Project Settings
                    -> Project
                        -> Project Language Level
    

Sometimes, we need to make sure to align the version for the Java Compiler as well:

    
        IntelliJ IDEA -> Preferences 
                        -> Build, Execution and Deployment 
                            -> Compiler 
                                -> Java Compiler 
                                    -> Project bytecode version
    

Another solution would be removing the maven.compiler.source property and set maven.compiler.target to the same version (1.8 in our case).

Fixing Invalid Source Release Error in Eclipse

Now, let’s see how to reproduce and solve the error in Eclipse IDE.

First, we will set the maven compiler source to Java 18:

    
        <maven.compiler.source>18</maven.compiler.source>
    

Next, we are going to select jdk1.8 as the default JRE in Eclipse:

Project JRE version in eclipse

Now, let’s run the mvn clean install command and view the logs:

    
        [INFO] BUILD FAILURE
        [INFO] ------------------------------------------------------------------------
        [ERROR] Failed to execute goal org.apache.maven.plugins:... 
                ...
                Fatal error compiling: invalid source release: 18 -> [Help 1]
    

As we can see, the build fails with the excat same error.

So, to fix the issue, we need to make sure that the JRE version of the project is greater than or equal to the one specified in maven.compiler.source.

Conclusion

In this short article, we covered in-depth the java: error: invalid source release: 18.

We see how to reproduce and fix the error both in IntelliJ and Eclipse.