13

We just upgraded the project from jdk 1.6 to jdk 1.8. While building the project in my machine, i'm getting following error.

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.3:compile (default-compile) on project exception: Fatal error compiling: invalid target release: 1.8 -> [Help 1]

Here is the maven compiler plugin which is been used

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.3</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        </configuration>
  </plugin>

I looked at the many post and most of them were related with not having the correct version configured for java_home. I verified all of these aspects and couldn't find any of them alarming.

Java version -

 qadeersmsiphone:main pdubey$ java -version
 java version "1.8.0_51"
 Java(TM) SE Runtime Environment (build 1.8.0_51-b16)
 Java HotSpot(TM) 64-Bit Server VM (build 25.51-b03, mixed mode)

Maven Version -

qadeersmsiphone:main pdubey$ mvn -version
Apache Maven 3.1.1 (0728685237757ffbf44136acec0402957f723d9a; 2013-09-17 08:22:22-0700)
Maven home: /usr/share/maven
Java version: 1.8.0_51, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.8.5", arch: "x86_64", family: "mac"

and I checked that while building the project maven uses jdk 1.8

qadeersmsiphone:main pdubey$ mvn install -debug
Apache Maven 3.1.1 (0728685237757ffbf44136acec0402957f723d9a; 2013-09-17 08:22:22-0700)
Maven home: /usr/share/maven
Java version: 1.8.0_51, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.8.5", arch: "x86_64", family: "mac"
[INFO] Error stacktraces are turned on.
[DEBUG] Reading global settings from /usr/share/maven/conf/settings.xml
[DEBUG] Reading user settings from /Users/pdubey/.m2/settings.xml
[DEBUG] Using local repository at /Users/pdubey/.m2/repository
[DEBUG] Using manager EnhancedLocalRepositoryManager with priority 10.0 for  /Users/pdubey/.m2/repository
[INFO] Scanning for projects...

UPDATE: I could get it working by removing the maven-compiler-plugin in pom file (Ideally, i don't want to do this). And i also observed that even if i remove this plugin, maven by default downloads the 3.3 version of compiler plugin in my local repo. I'm not sure what's wrong in this plugin although source and target appears to be correct.

Mihriban Minaz
  • 3,043
  • 2
  • 32
  • 52
Pankaj
  • 3,512
  • 16
  • 49
  • 83
  • 1
    Really? “`Apache Maven 3.1.1 (… 2013-09-17…`” – Holger Jul 23 '15 at 17:05
  • @Holger don't know what's the significance of (..2013-09-17) but i downloaded the maven from here https://archive.apache.org/dist/maven/maven-3/3.1.1/binaries/. Note that I had mac's default maven (v3.0.3) earlier which i upgraded to v3.1.1 – Pankaj Jul 23 '15 at 17:46
  • 2
    It simply looks older than Java 8, but, of course, that wouldn’t be a problem, as long as the compiler plugin and the invoked compiler can handle the requested target release 1.8, which they obviously can’t. But since the Maven version is the only version number you have posted, I assume that everything else is as ancient as that. I don’t know what other obstacles are lying in your environment, but the *first* thing I would try in your situation is to download from *here* instead: http://archive.apache.org/dist/maven/maven-3/3.3.3/binaries/ – Holger Jul 23 '15 at 17:54
  • For me, upgrading from Maven 3.2.5 to 3.3.3 did the trick. – Benj Jun 27 '16 at 13:53

6 Answers6

16

Check the java and Javac version - My Java version was 1.8.0_51

$ java -version
java version "1.8.0_51"

But javac version was pointing to old jdk 1.7.0_67

$ javac -version
javac 1.7.0_67

There are couple of thing which we should try -

  1. Ensure that symbolic link is pointing to correct jdk. If not, remove existing link and create new one for your jdk

    cd /System/Library/Frameworks/JavaVM.framework/Versions/
    rm CurrentJDK
    ln -s /Library/Java/JavaVirtualMachines/<jdk version>.jdk/Contents/ CurrentJDK
    
  2. In /Library/Java/Extension, i had a tools.jar. I believe, this was from old jdk.

    cd /Library/Java/Extensions
    -rw-r--r--  1 root  admin  15286304 Jan 14  2015 tools.jar
    

    Remove this

    sudo rm -rf /Library/Java/Extensions
    

point 2 did the trick for me :)

Pankaj
  • 3,512
  • 16
  • 49
  • 83
7

Sometimes this problem is related to a JAVA_HOME used by maven.

  • In my particular case I tried to start an install target and had an exact the subj message.
  • Then I just tried an mvn -version command and I realized that it uses jdk7
  • Then I run in a console set JAVA_HOME=C:\Java\jdk8 (my jdk path, so your own could be different)
  • Then typed mvn clean install test and it worked just fine at last

Good luck!

Lord Nighton
  • 1,670
  • 21
  • 15
  • That was it for me `mvn` used another version. If you are using powershell run this: `$env:JAVA_HOME="C:\java\jdk8` – papanito Sep 11 '19 at 12:59
2

Same issue here, but I was using run configurations inside Eclipse when maven kept throwing this error. Fixed by setting the right JDK version here:

Run Configuration image

Zoe
  • 27,060
  • 21
  • 118
  • 148
0

I had the same issue:

> java -version
java version "1.8.0_111"

>javac -version
javac 1.7.0.6

I checked the Path in System variables, and the jdk was not updated to jdk 8. After I updated the path with the right jdk it worked.

Guillaume Jacquenot
  • 11,217
  • 6
  • 43
  • 49
0

In cent os we can use alternatives to switch the version.

alternatives --config javac
alternatives --config java

But MAVEN uses JAVA_HOME.

And this JAVA_HOME can be possible to a link.

Either we have to point the JAVA_HOME manually to new jdk folder, OR manually remove/delete the link and create a new same link again from above answers.

From alternatives we can get the path of jdk

Kanagavelu Sugumar
  • 18,766
  • 20
  • 94
  • 101
0

First of all, it is definitely versions issue.

In my situation: java -version showed the result

java version "1.8.0_172"

and javac -version was showing

javac 1.8.0_172

BUT: Maven uses JAVA_HOME environment variable to get your JDK version to build a project and run it's a plugin. So the way how we have to check it is

echo %JAVA_HOME%

Which in my situation was

jdk1.7.0_172

So simply change the path to the variable will solve the issue. Hope it will save time to someone.

Kostiantyn
  • 81
  • 1
  • 4