Recently I created a new Maven project in Intellij Idea 2019 and when I compiled it, I got the following error as shown in the below screenshot.
The error message was: Error:java: error: release version 5 not supported. There can be many other issues such as java:string in switch are not supported, java:diamond operator is not supported in -source 1.5 and so on.
What is the issue
This happens because Maven sets the default Java version to 1.5 which is very old. According to the Maven website:
“Apache Maven Compiler Plugin
The Compiler Plugin is used to compile the sources of your project. Since 3.0, the default compiler is javax.tools.JavaCompiler (if you are using java 1.6) and is used to compile Java sources. If you want to force the plugin using javac, you must configure the plugin option forceJavacCompilerUse.
Also note that at present the default source setting is 1.5 and the default target setting is 1.5, independently of the JDK you run Maven with. If you want to change these defaults, you should set source and target as described in Setting the -source and -target of the Java Compiler.”
To fix the issue we have to specify which Java version is to be used.
This is the solution
According to the Maven Website, what we have to do is to add the following lines of code to the pom.xml file. Under source and target I have used Java version 11, change it to the version of java you are using.
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>11</source> <target>11</target> </configuration> </plugin> </plugins> </build>
You pom.xml file should look something like this:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>your project group ID</groupId> <artifactId>your project artifact ID</artifactId> <version>1.0-SNAPSHOT</version> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>11</source> <target>11</target> </configuration> </plugin> </plugins> </build> </project>
In Intellij, you can check which version of Java you are using by going to File -> Project Structure and under Project settings, Project as shown in the below screenshot.
Thats it, happy coding.