171

I am getting the following error on $ mvn compile:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project Sym360: Compilation failure: Compilation failure: 
[ERROR] Source option 5 is no longer supported. Use 6 or later.
[ERROR] Target option 1.5 is no longer supported. Use 1.6 or later.
[ERROR] -> [Help 1]

Here is the code of my pom.xml:

<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/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.test.app</groupId>
 <artifactId>Tset</artifactId>
 <packaging>jar</packaging>
 <version>1.0-SNAPSHOT</version>
 <name>Test</name>
 <url>http://maven.apache.org</url>

 <properties>
   <maven.compiler.source>6</maven.compiler.source>
   <maven.compiler.target>1.6</maven.compiler.target>
 </properties>

<build>
<pluginManagement>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.22.1</version>
    </plugin>
  </plugins>
</pluginManagement>
</build>
<dependencies>

<!-https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium- 
java -->
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.14.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.14.3</version>
    <scope>test</scope>
</dependency>

I tried to add properties in the pom.xml code, but still getting the same error.

Rafael Tavares
  • 5,678
  • 4
  • 32
  • 48
SUPARNA SOMAN
  • 2,311
  • 3
  • 19
  • 35
  • 1
    `Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile` this is a pretty old version. Also consider to move to 3.8.0. And in that case you can start using the property `maven.compiler.release = 6`, which is the preferred way to compile, though requires JDK 9 and above – Robert Scholte Oct 28 '18 at 19:03
  • I tried adding org.apache.maven.plugins maven-compiler-plugin 3.8.0 6 1.8 – SUPARNA SOMAN Oct 28 '18 at 19:22
  • But still getting the error @RobertScholte – SUPARNA SOMAN Oct 28 '18 at 19:22

23 Answers23

238

What helped me was these lines in pom.xml file

<properties>
     <maven.compiler.source>1.8</maven.compiler.source>
     <maven.compiler.target>1.8</maven.compiler.target>
</properties>
kraftwerk
  • 2,478
  • 2
  • 7
  • 6
  • 31
    Replace 1.8 with e.g. 11 if you target jdk 11 – JackLeEmmerdeur Oct 20 '20 at 18:39
  • 11
    I understand that this works, but I don't understand why maven uses a default source value that it does not support? – NobodyMan Sep 16 '21 at 22:17
  • 5
    I tracked this down a while ago. The reason is the default maven compiler plugin is very old and it defaults to version `1.5` which is why the error includes that version. If you include a newer compiler plugin (since 3.0) the default is JDK 1.7 so the problem goes away. For example, include plugin `org.apache.maven.plugins:maven-compiler-plugin:3.8.0` which defaults to Java `1.7` (see https://maven.apache.org/plugins/maven-compiler-plugin/) and the error goes away. Most will want `1.8` or `11` (or higher). – PatS Aug 19 '22 at 20:55
  • Here's another source about default source/target values (release doesn't have a default value): AbstractCompilerMojo.java:185 (see https://github.com/apache/maven-compiler-plugin/blob/5afdd48ade31aa59ef02ef1b5d093fd5032273b5/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java#L185) – Chris Oct 01 '22 at 15:20
  • 4
    @PatS `The reason is the default maven compiler plugin is very old and it defaults to version 1.5` Ok. But why Maven uses that very old plugin by default then? It's the same problem, just in a different place. Why does it use a broken, not working 15 years old or something plugin on latest Maven 3.8.6 build that I downloaded yesterday in 2022? It's not Ok and I'd argue it makes the whole package manager broken. I shouldn't get build errors when I try to build a HelloWorld Maven project with a barebones .pom file and a single file. It should just work and build when I run `mvn install`. – KulaGGin Nov 11 '22 at 11:51
  • @KulaGGin, I agree with you and found this frustrating. I believe the solution is to use a modern Maven 3.3+ which use JDK 1.7 (see https://maven.apache.org/download.cgi). Requiring JDK 1.7+ should also default to the newer `maven-compiler-plugin` that defaults to JDK 1.7. – PatS Nov 12 '22 at 14:39
40

Also in one of my IntelliJ IDEA projects, in addition to all of answers above, another try works: Just change Language level in Modules section of Project Structure [image below] Language Level

cellepo
  • 4,001
  • 2
  • 38
  • 57
Mohsen Abasi
  • 2,050
  • 28
  • 30
  • 18
    This is the kind of unexplainable voodoo that makes you want to switch carreer – Mehdi LAMRANI May 22 '20 at 18:05
  • 1
    Please specify what IDE this is. – ssimm Jan 08 '21 at 09:07
  • 1
    @ssimm This is Intellij Idea – Rajat Goel Dec 31 '21 at 11:01
  • 1
    This window is available if you open `File - Project Structure...` menu. This solution won't work in the long run. We're talking about the Maven project here. This option will be overwritten after Maven project will get reimported, and you'll need to reimport it after you change anything about the Maven project. Which is going to happen in a few minutes, hours or a few days, depending on how young your project is. The proper way to set this option is to set it in the .pom file in the ``: source and target options, as other answers suggest. – KulaGGin Nov 11 '22 at 12:16
35

I had same issue, the problem is with properties. Check your JavaSE version in your project, it will be displayed beside JRE System Library folder in your project. If it is 1.5, then it will throw an error. Most probably you will have a updated version, so check the version and update it. I have updated it below based on your code.

<properties>
   <maven.compiler.source>1.6</maven.compiler.source>
   <maven.compiler.target>1.6</maven.compiler.target>
 </properties>
  • Your suggestion worked for me! After adding the above tag, and clicking on 'Maven Install', all my maven dependencies mentioned in pom.xml file got downloaded and added to the project. – Jyotsna Masand Mar 05 '22 at 18:27
24

I had same issue and i have added below configuration in pom.xml and it works.

<build>
   <plugins>
   <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
    </plugin>
   </plugins>
   </build>
20

In Eclipse This helped me:

  1. Right Click on Project.
  2. Click on Build path.
  3. Click on Configure Build path.
  4. It opens a Java Build path window.
  5. Click on Java Compiler in the Left side.
  6. It navigates to Java Compiler window in that to set the Compiler compliance level is set as according to your jre version(ex if java version is 1.8 then choose 1.8) as select.
  7. Click on [Apply] button.
  8. Click on [OK] button.
  9. Right click on Project > Maven > Update the project.
  10. Right click on Project > Run As > Maven install -- The pom.xml file is running and java jars are download and installed to project.
  11. Right click on Project > Run As > Maven Test -- The pom.xml file is running and java jars are download and installed to project.

Then you got the Build Success message and your maven project is created successfully.

cellepo
  • 4,001
  • 2
  • 38
  • 57
SUPARNA SOMAN
  • 2,311
  • 3
  • 19
  • 35
  • 4
    Eclipse is capable to do this automatically. Step #9 should be the only thing to do, assuming the JDK supports 1.8 – Robert Scholte Oct 29 '18 at 17:27
  • I prefer this answer as I like GUI and i use Eclipse. it works for me. I finished step#6, but I didn't run Maven Update the project. thanks for your solution – Phil May 05 '22 at 02:25
18

I think you have wrong your pom.xml:

<properties>
   <maven.compiler.source>6</maven.compiler.source>
   <maven.compiler.target>1.6</maven.compiler.target>
 </properties>

change to:

<properties>
   <maven.compiler.source>1.6</maven.compiler.source>
   <maven.compiler.target>1.6</maven.compiler.target>
 </properties>

Now depending if you are using the command line use:

mvn clean compile

or either way(eclipse ide)

Right click on project Run with maven>build>Goal (compile)

Mauricio Toledo
  • 630
  • 7
  • 10
  • 2
    [Not correct]!!! Invalid source & target release "1.11". It must be 11 not 1.11 – Mohsen Abasi Dec 25 '19 at 05:53
  • I was getting same error. I was using java 11 and generating 1.8 compatible code. setting both properties to 1.8 fixed the issue. I used 'mvn clean package' command to create jar file. – Gopal Bairwa Feb 06 '20 at 22:25
13

adding below code in pom will resolve the issue

 <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>
  <profiles>  
Vinod Panthi
  • 131
  • 1
  • 3
  • Java 7 is 11 years old now and reached its end of service years ago: https://en.wikipedia.org/wiki/Java_version_history. – Gerold Broser Aug 21 '22 at 06:25
9

I was facing the same issue and resolved it with the lines of code below:

<properties>
   <maven.compiler.source>1.6</maven.compiler.source>
   <maven.compiler.target>1.6</maven.compiler.target>
 </properties>
Elletlar
  • 3,136
  • 7
  • 32
  • 38
Shahid Hussain Abbasi
  • 2,508
  • 16
  • 10
  • Java 6 is 16 years old now and reached its end of service years ago: https://en.wikipedia.org/wiki/Java_version_history. – Gerold Broser Aug 21 '22 at 06:25
5

Some people have asked:

I understand that this works, but I don't understand why maven uses a default source value that it does not support? –

The answer is:

  • Maven doesn't know what version of the JDK you will use.

  • When used with JDK 1.8 Maven compiles work successfully.

  • When using newer versions of the JDK, it's not clear what version of the byte code you want to use. So Maven's has punted and continues use what it has used for many years.

At this day and age, it is difficult for Maven (as a generic build tool) to select a default byte code version that everyone will like.

So it's probably best to get used to putting the version of your source code and the byte code you want to generate in your pom.xml file.

You can argue (and I would agree) that maven should (by default) use a newer version of the maven-compiler-plugin but as I stated, whatever version was picked, someone would have a problem with it.

Example

For example, if you use JDK 11, you might very well be using Java 11 syntax and need -source 11 -target 11 when compiling your code.

Even the most recent release of the plugin maven-compiler-plugin:3.10.1 defaults to JDK 1.7 syntax which would result in compilation errors for Java 11 code.

A full description of the problem

Other's have said this, but to be complete. The default pom.xml doesn't specify the maven-compiler-plugin. To find out the version used you can use

mvn help:effective-pom | more
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>

You'll see that the maven-compiler-plugin:3.1 is being used. You can see that this version generates a command with -target 1.5 -source 1.5 which generates an error when used with versions of Java newer than Java 1.8.

The following shows the error that occurs when using JDK 17.

mvn --version
Apache Maven 3.8.6 (84538c9988a25aec085021c365c560670ad80f63)
Maven home: D:\p\apache-maven-3.8.6
Java version: 17.0.2, vendor: Oracle Corporation, runtime: D:\p\jdk-17.0.2
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

mvn clean install -X
. . .
[DEBUG] -d D:\Play\maven\helloworld\target\classes -classpath D:\Play\maven\helloworld\target\classes; -sourcepath D:\Play\maven\helloworld\src\main\java; -g -nowarn -target 1.5 -source 1.5
. . .
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] Source option 5 is no longer supported. Use 7 or later.
[ERROR] Target option 5 is no longer supported. Use 7 or later.
[INFO] 2 errors

The fix

The fix was to update the maven pom.xml file to specify either a newer maven-compiler-plugin. I tested with maven-compiler-plugin:3.10.1 and it uses -target 1.7 -source 1.7

The syntax is:

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.10.1</version>
      </plugin>
    </plugins>
  </build>

At the time of this writing version 3.10.1 was the latest version.

The other option is to specify the version of the byte code you want to generate, one way is to use this (as stated in another answer):

<properties>
     <maven.compiler.source>1.8</maven.compiler.source>
     <maven.compiler.target>1.8</maven.compiler.target>
</properties>

This will work even with the old version 3.1 plugin.

PatS
  • 8,833
  • 12
  • 57
  • 100
4
I fixed this by adding this in pom.xml file:
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
ahmnouira
  • 1,607
  • 13
  • 8
4

After so many years, happily seeing the same error ! :P

[ERROR] Source option 5 is no longer supported. Use 7 or later. [ERROR] Target option 5 is no longer supported. Use 7 or later.

pom.xml did not have any source or target declarations, so maven must have taken some archaic versions as default, rather than being clever enough to get it from the install JVM (which is currently on 18).

So, without knowing the original issue, I could also resolve it through the below snippet below. That declares a specific source and target version, which subpresses the problem.

<build>
   <plugins>
   <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <source>18</source>
            <target>18</target>
        </configuration>
    </plugin>
   </plugins>
   </build>
  • You don't have to pollute your POM's ``section. See [my answer herein](https://stackoverflow.com/a/68091724/1744774): `...` should be sufficient. – Gerold Broser Aug 21 '22 at 04:54
  • Spent hours trying to figure out why the most popular suggestion in this thread didn't work for me. Turns out my pom.xml had a similar snippet to this yours at the beginning of the file, which I didn't initially notice. thanks! – jakechowder Apr 11 '23 at 20:53
3

For the new Apache net Bean its a little bit different from the suggestion by SUPARNA SOMAN

  • Right Click on your Project -Hover on "set configuration" and click customize configuration -.A new dialogue box opens....
  • At the left corner where the categories are, Click on "Source"
  • At the select form on the page below, select your required version of JDK ----see image for this last step.the last step required to change jdk version
eclat_soft
  • 602
  • 7
  • 9
3

On MacOS I have multiple versions

user> ls /Library/Java/JavaVirtualMachines/
jdk-11.0.4.jdk      jdk-12.0.2.jdk      jdk1.8.0_221.jdk

and JAVA_HOME was not defined properly so Maven used jdk-12. I have jdk-11,jdk-8, and jdk-12.

user> mvn -version
Apache Maven 3.6.1 
Maven home: /usr/local/Cellar/maven/3.6.1/libexec
Java version: 12.0.2, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk-12.0.2.jdk/Contents/Home
Default locale: XXX, platform encoding: UTF-8
OS name: "mac os x", version: "10.14.6", arch: "x86_64", family: "mac"

So:

Define JAVA_HOME to use jdk-8.

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home

Try again, now maven is:

user> mvn -version
    Maven home: /usr/local/Cellar/maven/3.6.1/libexec
    Java version: 1.8.0_221, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre

and the build is:

[INFO] BUILD SUCCESS
Adelin
  • 18,144
  • 26
  • 115
  • 175
2

None of the solutions above worked for me.

For some reasons the file with the name of my project.iml had changed. Found the difference in the previous subversion repository submission...

In projectname.iml I found this line:

<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_5">

And all I had to do was changing it to 11

<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_11">
Dan
  • 530
  • 6
  • 22
2

This is a message from a newer javac, e.g.:

$ java -version
openjdk version "11" 2018-09-25

$ javac -source 1.5 -target 1.5 Test.java
error: Source option 5 is no longer supported. Use 6 or later.
error: Target option 1.5 is no longer supported. Use 1.6 or later.

So, apparently you're using a newer JDK version with a Maven version prior to 3.8.0 ("<source>/<target> ... NOTE: Since 3.8.0 the default value has changed from 1.5 to 1.6"). The maven-compiler-plugin:3.1 you use is from April 2013.

There are two possibilities:

  1. Update your Maven version to the latest (I'd recommend that)

  2. Setting the Java Version in Maven:

2.2. Java 9 and Beyond

...

<properties>
   <maven.compiler.release>...</maven.compiler.release>
</properties>
Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
1

Here is the solution which helped me:

I had the same issue on error source option 5 is no longer supported, Use 6 or later

So i followed these instructions and problem SOLVED

  1. Open Project Properties (File Menu)
  2. Change the Source / Binary Format to the latest version (JDK 7 in my case)

Project Properties enter image description here

Source / Binary Format enter image description here

Clean and Build, Then Run the project

Surya R Praveen
  • 3,393
  • 1
  • 24
  • 25
0

If in Eclipse, Write click on project and go to properties. Search for maven and configure a jdk higher version (1.7 onwards) there and apply. Now try maven install.

Aditi Mane
  • 11
  • 3
0

In my case, running MacOS Big Sur and JDK version 15, I added the code below in pom.xml file shown below. I added the 15 for my JDK version.

<properties>
   <maven.compiler.source>15</maven.compiler.source>
   <maven.compiler.target>15</maven.compiler.target>
 </properties>

I then re-ran $ mvn clean wildfly:deploy and it worked.

daktari
  • 91
  • 1
  • 8
0

Both options work for me to resolved Source option 5 is no longer supported. Use 6 or later” on Maven compile

Open pom.xml file

Option1: add build tags

Option2: add properties tags

<project>
    <groupId>com.pluralsight</groupId>
    <artifactId>HelloWorld</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modelVersion>4.0.0</modelVersion>
    <packaging>jar</packaging>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>12</release>
                </configuration>
            </plugin>
        </plugins>
    </build>    
</project>

OR

<project>
    <groupId>com.pluralsight</groupId>
    <artifactId>HelloWorld</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modelVersion>4.0.0</modelVersion>
    <packaging>jar</packaging>

    <properties>
         <maven.compiler.source>1.6</maven.compiler.source>
         <maven.compiler.target>1.6</maven.compiler.target>
    </properties> 
</project>
  • Please add some comments about your code and how does it answers the question. – Cosmin Staicu Mar 31 '21 at 05:46
  • 1) See [my answer herein](https://stackoverflow.com/a/68091724/1744774): With Java >=9 there is `...`. 2) Short for (XML) "_tags_" is "[_(XML) element_](https://www.w3schools.com/xml/xml_elements.asp)" ;). 3) Your declarations of `12` vs. `<...source>1.6<...target>1.6<...>` contradict each other. (See also last link in my answer.) Such you're relying on the order of POM evaluation which is unkwown to us Maven users and which is bad (since POMs are of declarative not imperative nature). – Gerold Broser Aug 21 '22 at 05:07
0

Change the

  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">

to

  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_11">

enter image description here

Tiago Medici
  • 1,944
  • 22
  • 22
0

Set updated java version from build path as per you installed

ChinmoyJ
  • 61
  • 5
0

I solve the problem by finding that my $JAVA_HOME is empty. After setting it, compilation succeeded!

Herb
  • 91
  • 1
  • 5
0

If you are getting similar kind of error, just change the version as I mentioned below in my pom.xml file:

pom.xml

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • Please add code and data as text ([using code formatting](//stackoverflow.com/editing-help#code)), not images. Images: A) don't allow us to copy-&-paste the code/errors/data for testing; B) don't permit searching based on the code/error/data contents; and [many more reasons](//meta.stackoverflow.com/a/285557). Images should only be used, in addition to text in code format, if having the image adds something significant that is not conveyed by just the text code/error/data. – Adrian Mole Mar 17 '23 at 08:34